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.

Please set the playground first

Loading "Generic Functions"
Loading "Generic Functions"