TypeScript · Middle · Question 31 of 75
How does unknown[] differ from any[] in a function parameter?
- ✓Correct answer: With
unknown[]elements must be narrowed, withany[]they don't - 2
unknown[]accepts only arrays of objects, not primitives - 3No difference: both disable checks for the elements
Explanation
Both accept any array, but inside a function with unknown you can't call an element's method without a check. If the function doesn't work with the elements (e.g. counts the length), unknown[] is the safe choice. Even better is a generic T[] if the type matters for the result.