Generic Functions
Generic Functions (π solution)
π¨βπΌ Your functions now work with any type!
π¦ Notice how TypeScript infers the type parameter from the arguments:
first([1, 2, 3]) // TypeScript infers Item = number
first(['a', 'b']) // TypeScript infers Item = string
You rarely need to explicitly specify the type parameterβTypeScript figures it
out. But you can when needed:
first<number>([]) // Explicitly Item = number
The return types are also automatically correct:
first([1, 2, 3]) returns
number | undefined, not any.

