Conditional Types Flashcards

1
Q

What are conditional types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Where can we use conditional types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define a conditional type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of conditional types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you provide an example of a conditional type?

A
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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the infer keyword do in Conditional Types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are distributed conditional types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly