typescript / intermediate
Snippet
Conditional Types
Conditional types select one of two possible types based on a condition expressed as a type relationship test.
snippet.ts
1
2
3
type IsString<T> = T extends string ? "Yes" : "No";type Result = IsString<number>; // "No"
Breakdown
1
T extends string
Checks if the generic type T can be assigned to a string type.
2
? "Yes" : "No"
If true, the resulting type is the literal "Yes", otherwise it is "No".