Leçon Chapitre 4 · Unions et affinage · épisode 1 sur 1
Unions et affinage
Une valeur qui peut avoir plusieurs types, et comment les distinguer.
TypeScript
1function longueur(x: string | string[]): number {2 if (typeof x === "string") {3 return x.length // ici, x est une string4 }5 return x.length // ici, un tableau6}Ensuite · Leçon