All Challengesintermediate
TypeScript generics basics
Generics appear in almost every TypeScript codebase. AI uses them to write reusable functions.
typescriptgenericstype inferencereusability
typescript
function getFirst<T>(arr: T[]): T | undefined {
return arr[0]
}
function wrap<T>(value: T): { data: T; timestamp: number } {
return { data: value, timestamp: Date.now() }
}
const num = getFirst([1, 2, 3])
const str = getFirst(['a', 'b', 'c'])
const empty = getFirst([])Question
What are the TypeScript types of num, str, and empty?