February 2019
Beginner
694 pages
18h 4m
English
The keyof keyword and generic types allow us to do some rather interesting theoretical type mathematics, and map one type to another based on some simple rules. This is best illustrated through a number of code examples, as follows:
interface IAbcRequired {
a: number;
b: string;
c: boolean;
}
type PartialProps<T> = {
[K in keyof T]?: T[K];
}
Here, we have defined an interface named IAbcRequired, that has three properties, named a, b, and c. We then define a type named PartialProps, which is using generic syntax that requires a type named T. This PartialProps type defines a single property, named K, which is of type keyof T.
In other words, take all of the properties of type T, and include them in this new type named PartialProps ...
Read now
Unlock full access