Grokking TypeScript
0% completed
Conditional Types
A ternary expression in JavaScript chooses between two values. A conditional type does the same job for types, and the two have nearly the same syntax.
const value: unknown = "hello"; // A ternary chooses a value at runtime const label = typeof value === "string" ? "Yes" : "No"; // A conditional type chooses a type at compile time type IsString<T> = T extends string ? "Yes" : "No"; type Result1 = IsString<string>; // "Yes" type Result2 = IsString<number>; // "No"
The ternary runs when the program runs
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%