You are viewing a free preview of this lesson.
Subscribe to unlock all 12 lessons in this course and every other course on LearningBro.
TypeScript's type system is remarkably expressive. Beyond basic types and generics, it provides mapped types, conditional types, the infer keyword, and a rich set of built-in utility types. These advanced features allow you to create types that transform other types, enforce complex constraints, and eliminate boilerplate.
Mapped types create new types by transforming each property of an existing type:
// Make all properties optional
type MyPartial<T> = {
[K in keyof T]?: T[K];
};
// Make all properties readonly
type MyReadonly<T> = {
readonly [K in keyof T]: T[K];
};
// Make all properties nullable
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
interface User {
id: number;
name: string;
email: string;
}
type PartialUser = MyPartial<User>;
// { id?: number; name?: string; email?: string }
type ReadonlyUser = MyReadonly<User>;
// { readonly id: number; readonly name: string; readonly email: string }
You can remap keys in mapped types using the as clause:
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
Subscribe to continue reading
Get full access to this lesson and all 12 lessons in this course.