TypeScript · Junior · Question 16 of 75

What is a generic?

Explanation

function first<T>(arr: T[]): T | undefined - the result type is linked to the input array's type: first([1, 2]) returns number | undefined. With any, that link is lost. Generics are used in Array<T>, Promise<T>, useState<T>.

Further reading

Problem with a question

TypeScript · Junior · question 16

Test yourself

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

Take this quiz