Learn TypeScript

Quiz

Quiz

This quiz will test what you have learned about conditional types.

1.

What type is Check equivalent to in the code below?

type Animal = {
name: string;
legs: number
}
type Check = Animal extends { name: string } ? "yes" : "no";
2.

We have a requirement to create a utility type called NullableProps that gets all the nullable properties from an object type. How could this be defined?

type NullableProps<T> = {
[P in keyof T]: P
}[keyof T];
type NullableProps<T> = {
[P in keyof T]: null extends T[P] ? never : P
}[keyof T];
type NullableProps<T> = {
[P in keyof T]: null extends T[P] ? P : never
}[keyof T];
3.

We have a requirement to create a utility type similar to ReturnType with the improvement of handling asynchronous functions. How could we define this type?

type ReturnTypeAsync<
T extends (...args: any) => any
> = T extends (...args: any) => Promise<infer R>
? R
: T extends (...args: any) => infer R
? R
: any;
type ReturnTypeAsync<
T extends (...args: any) => any
> = T extends (...args: any) => infer R
? R
: any;
type ReturnTypeAsync<
T extends (...args: any) => any
> = T extends (...args: any) => Promise<infer R>
? R
: any;

Prev

Creating some standard conditional utility types

Next

Introduction

© 2024 Carl Rippon
Privacy Policy
This site uses cookies. Click here to find out more