TypeScript · Junior · Question 16 of 75
What is a generic?
- ✓Correct answer: A type parameter that lets you write code for different types while keeping the link between them
- 2The
anytype, used to temporarily turn off checks in functions - 3A base class that all other types and interfaces inherit from
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>.