Conditional Types Flashcards
What are conditional types?
Conditional types in TypeScript are a way to express non-uniform type mappings, allowing you to create a type that represents different types based on a condition. Conditional types are defined using the ternary conditional operator T extends U ? X : Y
.
“Documentation - Conditional Types” (typescriptlang.org). Retrieved April 4, 2023.
Where can we use conditional types?
Conditional types can be used in various places, such as function return types, class properties, and generic type arguments.
They are often used in situations where a type depends on the input type or other conditions.
“Documentation - Conditional Types” (typescriptlang.org). Retrieved April 4, 2023.
How do you define a conditional type?
Here is the syntax for a conditional type:
T extends U ? X : Y
T extends U
is the condition, which checks if type T
is a subtype of type U
.X
is the resulting type when the condition is true.Y
is the resulting type when the condition is false.
An example using conditional types in a function:
type Flatten<T> = T extends Array<infer U> ? U : T; function doSomething<T>(input: T): Flatten<T> { // Implementation here }
In this example, the Flatten
type checks if T
is an array. If it is, it returns the element type U
; otherwise, it returns T
unchanged. The doSomething
function uses this conditional type to determine its return type based on its input type.
“Documentation - Conditional Types” (typescriptlang.org). Retrieved April 4, 2023.
What is the purpose of conditional types?
Conditional types allow you to create complex type relationships and transformations based on conditions. They are useful for:
1.- Defining types based on the properties or structure of other types.
2.- Filtering types, such as extracting specific property types from an object type.
3.- Defining types that depend on the values of other types.
“Documentation - Conditional Types” (typescriptlang.org). Retrieved April 4, 2023.
Can you provide an example of a conditional type?
type IsString<T> = T extends string ? "yes" : "no"; let strTest: IsString<string>; // "yes" let numTest: IsString<number>; // "no"
In this example, the IsString type checks if the given type T is a string. If T is a string, the resulting type will be “yes”, otherwise it will be “no”.
“Documentation - Conditional Types” (typescriptlang.org). Retrieved April 4, 2023.
What does the infer
keyword do in Conditional Types?
The infer
keyword in conditional types allows you to introduce a type variable within the scope of a conditional type expression. It is used to infer and extract a specific type from a given input type, which can then be used within the true or false branches of the conditional type.
Example:
type ElementType<T> = T extends Array<infer U> ? U : never;
In this example, the ElementType
type checks if T
is an array. If it is, the infer U
keyword extracts the element type from the array, and the resulting type is U
. If T
is not an array, the resulting type is never
.
“Inferring Within Conditional Types” (typescriptlang.org). Retrieved April 10, 2023.
What are distributed conditional types?
Distributed conditional types refer to a specific behavior of conditional types in TypeScript, where a union type is distributed over the conditional type. When a conditional type encounters a union type as its checked type (the type being tested in the condition), it automatically distributes the conditional type over each member of the union type, resulting in a new union type with the conditional type applied to each member.
Example:
type Flatten<T> = T extends Array<infer U> ? U : T; type InputType = string[] | number[] | boolean; type OutputType = Flatten<InputType>; // string | number | boolean
In this example, InputType
is a union type, and the Flatten
conditional type is distributed over each member of the union. The resulting OutputType
becomes a union of the types produced by applying the Flatten
conditional type to each member of the InputType
union, which is string | number | boolean
.
“Distributive Conditional Types” (typescriptlang.org). Retrieved April 10, 2023.