Like a labeled container — it remembers what type you put in and guarantees you get the same type back.
T is a type variable — a placeholder that TypeScript fills in at call site. identity<string> makes T=string everywhere in the signature.
> identity<string>("hello") → "hello" : string
> identity(42) → 42 : number (T inferred)
> No runtime cost — generics are erased to JSLike a labeled container — it remembers what type you put in and guarantees you get the same type back.
T is a type variable — a placeholder that TypeScript fills in at call site. identity<string> makes T=string everywhere in the signature.
> identity<string>("hello") → "hello" : string
> identity(42) → 42 : number (T inferred)
> No runtime cost — generics are erased to JSSign in to cast your vote
Sign in to share your feedback and join the discussion.