[TypeScript] Mapped Type
[Typescript] Identity function - Reverse mapped types with identity function 02
import { Equal, Expect } from '../helpers/type-utils'; export function makeEventHandlers< T extends { [Key in keyof T]: (key: Key) => void } >(obj: T) { return obj; } const obj = makeE
日期 2023-06-12 10:48:40[TypeScript] Mapped Type
Index Signature type Fruit = { name: string color: string mass: number } type Dict<T> = { [k: string]: T } // <- index signature const fruitCatalog: Dict<Fruit> = {} fruitCat
日期 2023-06-12 10:48:40[Typescript] Key remapping in mapped types by using `as` keyword V4.2
Since TypeScript v4.2, we can do remapping by using `as` keyword type Colors = "red" | "green" | "blue" type ColorSelector = { [K in Colors as `select${Capitalize<K>}`]: () => void
日期 2023-06-12 10:48:40[Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
we're going to dive deep into a more complex example in which we combine mapped types, key remapping, template literal types, and indexed access types to statically type a highly dynamic JavaScript f
日期 2023-06-12 10:48:40[Typescript] “Record” Mapped Type
"Record" repersent key-value pair. type MyRecord<K extend string, T> = { [P in K]: T } Record key should be string. array[0] in javascript, even we give array 0 as key, it
日期 2023-06-12 10:48:40[Typescript] “Required” Mapped Type, +/- Modifiers
For example we have interface: interface Person { name: string, age?: number } 'age' is an optional prop. // will have an error since person.age is used function printAge(person) {
日期 2023-06-12 10:48:40[Typescript] “Partial” Mapped Type
type MyPartial<T> = { [P in keyof T]?: T[P] } 'in' like a loop. Usage: function updatePerson(person: Person, prop: MyPartial<Person>) { return {...person, ...prop}
日期 2023-06-12 10:48:40[TypeScript] "Pick" Mapped Type
interface Person { name: string; age: number; address: {} } // gives error since we didn't define 'address' const person: Person = { name: "Wan", age: 30 } We can use 'P
日期 2023-06-12 10:48:40[Typescript] "Readonly" Mapped Type
interface Person { name: string; age: number; } interface ReadonlyPerson { readonly name: string; readonly age: number; } const person: ReadonlyPerson { name: "Wan" age: 28
日期 2023-06-12 10:48:40[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from
日期 2023-06-12 10:48:40[TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create a new type by transforming all properties of an existing type according to a given transforma
日期 2023-06-12 10:48:40[Typescript] Identity function - Reverse mapped types with identity function 02
import { Equal, Expect } from '../helpers/type-utils'; export function makeEventHandlers< T extends { [Key in keyof T]: (key: Key) => void } >(obj: T) { return obj; } const obj = makeE
日期 2023-06-12 10:48:40[TypeScript] Mapped Type
Index Signature type Fruit = { name: string color: string mass: number } type Dict<T> = { [k: string]: T } // <- index signature const fruitCatalog: Dict<Fruit> = {} fruitCat
日期 2023-06-12 10:48:40[Typescript] Key remapping in mapped types by using `as` keyword V4.2
Since TypeScript v4.2, we can do remapping by using `as` keyword type Colors = "red" | "green" | "blue" type ColorSelector = { [K in Colors as `select${Capitalize<K>}`]: () => void
日期 2023-06-12 10:48:40[Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
we're going to dive deep into a more complex example in which we combine mapped types, key remapping, template literal types, and indexed access types to statically type a highly dynamic JavaScript f
日期 2023-06-12 10:48:40[Typescript] “Required” Mapped Type, +/- Modifiers
For example we have interface: interface Person { name: string, age?: number } 'age' is an optional prop. // will have an error since person.age is used function printAge(person) {
日期 2023-06-12 10:48:40[Typescript] “Partial” Mapped Type
type MyPartial<T> = { [P in keyof T]?: T[P] } 'in' like a loop. Usage: function updatePerson(person: Person, prop: MyPartial<Person>) { return {...person, ...prop}
日期 2023-06-12 10:48:40[TypeScript] "Pick" Mapped Type
interface Person { name: string; age: number; address: {} } // gives error since we didn't define 'address' const person: Person = { name: "Wan", age: 30 } We can use 'P
日期 2023-06-12 10:48:40[Typescript] "Readonly" Mapped Type
interface Person { name: string; age: number; } interface ReadonlyPerson { readonly name: string; readonly age: number; } const person: ReadonlyPerson { name: "Wan" age: 28
日期 2023-06-12 10:48:40[Typescript] “Record” Mapped Type
"Record" repersent key-value pair. type MyRecord<K extend string, T> = { [P in K]: T } Record key should be string. array[0] in javascript, even we give array 0 as key, it
日期 2023-06-12 10:48:40[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from
日期 2023-06-12 10:48:40[TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create a new type by transforming all properties of an existing type according to a given transforma
日期 2023-06-12 10:48:40