Skip to main content
PProfileMatch
← Path

TypeScript · TypeScript Generics Explained: Functions, Interfaces and Constraints

0 XP
  1. 1 current stop: learning
  2. 2 upcoming
  3. 3 upcoming
  4. 4 upcoming
  5. 5 upcoming

Finish after stop 5

Stop 1 of 5 · Library returns desk

Trick to keep in mind+3 XP

Generic = same type in, same type out, with a blank for TypeScript to fill

In firstOr<T>(items: T[], fallback: T): T, the T is a blank. TypeScript fills it in from the arguments you actually pass, and the return type uses the same filled-in value. That link between input and output is what a plain any cannot give you.

Read it once, then a question tests whether the trick stuck.