Quiz · Generics · lesson 2 of 10Quiz — why genericsWhat are generics in TypeScript and why are they useful?[A]A generic function wraps every value in a run-time wrapper object that keeps the name of its type, and the JavaScript reads that name to pick the right branch of code. So one function serves many types, but a generic call runs slower than a plain one, so keep it out of hot loops.[B]A generic function declares a type parameter T that each call fills with a real type; classes and types can be generic too. The code serves many types and keeps the exact one: first<T>(xs: T[]): T | undefined gives number | undefined for number[]; any loses it.[C]A generic is another way to write any: first<T>(xs: T[]): T | undefined and first(xs: any[]): any are checked the same, because tsc drops the angle brackets at once. So first([1, 2]) gives any every time, and T is a note for the reader.[D]A generic function accepts only the built-in primitives string, number and boolean, and tsc picks one of them by itself at each call. Your own object types and classes cannot take the place of T, so for each of them you write a separate overload by hand.← BackGeneric functionsNext →How T is inferred