TypeScript Conditional Types
- typescript
A conditional type follows the ternary pattern:
The main use case for a conditional type is when we want to declare the return type of a function, and that return type depends on the type of the input.
Suppose we have the createLabel
function which takes either a string name or a number ID, and then returns either a name label object in the case of a string input, or a ID label object in the case of a number input:
This is pretty cumbersome since we need to create three overloads for the createLabel
function. If we add another input type then we'd have to create even more overloads.
Instead we can create a conditional type:
And then instead of needing the overloads we can simply declare the function as: