MutterCorp
MutterCorpNotice · Notícias
Programação·

TypeScript Conditional Types Explained: The Complete Guide

You write a type alias meant to wrap anything in an array: type ToArray = T extends any ? T[] : never. Feed it string, you get string[]. Feed it string | number, you reasonably expect (string | number)[]. What you actually get is string[] | number[] — a union of two separate array types, not a single array of the union. The alias is correct. TypeScript is correct. You've just run into distribution, one of the specific, learnable rules behind conditional types — the mechanism TypeScript uses to…

Publicado no Notice. Categoria: Programação.

Notícias sobre o mesmo assunto em outras fontes