TypeScript · Middle · Question 31 of 75

How does unknown[] differ from any[] in a function parameter?

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.

Further reading

Problem with a question

TypeScript · Middle · question 31

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz