Explainers · Short
TypeScript infer reads your route string
- Published
- Duration
- Language
- English
Transcript
Text of the video's narration.
TypeScript can pull a type out of a string. That is infer. IsParam asks whether a string starts with a colon. The pattern is a colon, then a hole named Name.:id fills that hole with id. users matches nothing, so you get never. extends is the pattern; infer is what TypeScript fills in.
Params walks a route slash by slash and keeps only those segments. From /users/:id/posts/:postId, you get exactly id or postId. A route function types the params object from that path. Pass id and postId, and it compiles. Pass postId where the path only has id, and it does not compile.
Pass an empty object, and TypeScript tells you id is missing. The compiler reads your string.
Sources
Credits and notes
- Narrated with Phonon; motion in Remotion.



