Pastāstiet man par utilitārajiem tipiem TypeScript.
sobes.tech AI
Atbilde no AI
TypeScript noderīgo tipu izmantošana ir efektīvs veids, kā pārvaldīt sarežģītus datu tipus. Šie iebūvētie tipi ļauj pārveidot esošos tipus jaunajos, veikt dažādas noderīgas operācijas un uzlabot koda elastību un atkārtotu izmantošanu. Galvenie noderīgie tipi ir:
-
Partial<Type>: Izveido jaunu tipu, kurā visas
Typeīpašības ir izvēles.interface User { id: number; name: string; age: number; } type PartialUser = Partial<User>; // PartialUser būs { id?: number; name?: string; age?: number; } -
Readonly<Type>: Izveido jaunu tipu, kurā visas
Typeīpašības ir tikai lasāmas.interface Point { x: number; y: number; } type ReadonlyPoint = Readonly<Point>; // ReadonlyPoint būs { readonly x: number; readonly y: number; } -
Pick<Type, Keys>: Izveido tipu, izvēloties noteiktas
Keysīpašības noType.interface Product { id: number; name: string; price: number; description: string; } type ProductSummary = Pick<Product, 'id' | 'name' | 'price'>; // ProductSummary būs { id: number; name: string; price: number; } -
Omit<Type, Keys>: Izveido tipu, izslēdzot noteiktas
Keysīpašības noType.interface Order { id: number; productId: number; quantity: number; timestamp: string; } type OrderDetails = Omit<Order, 'timestamp'>; // OrderDetails būs { id: number; productId: number; quantity: number; } -
Exclude<Type, ExcludedUnion>: Izveido tipu, izslēdzot no
Typeelementus, kas var tikt piešķirtiExcludedUnion.type Colors = 'red' | 'green' | 'blue' | 'yellow'; type PrimaryColors = Exclude<Colors, 'yellow'>; // PrimaryColors būs 'red' | 'green' | 'blue' -
Extract<Type, Union>: Izveido tipu, izvēloties no
Typeelementus, kas var tikt piešķirtiUnion.type AllAnimals = 'dog' | 'cat' | 'fish' | 'bird'; type PetAnimals = Extract<AllAnimals, 'dog' | 'cat'>; // PetAnimals būs 'dog' | 'cat' -
NonNullable<Type>: Izveido tipu, izslēdzot no
Typenullunundefined.type MaybeString = string | null | undefined; type NotNullableString = NonNullable<MaybeString>; // NotNullableString būs string -
Record<Keys, Type>: Izveido objektu tipu, kura īpašības ir no
Keysun vērtības irType.type CityPopulation = Record<string, number>; // CityPopulation būs { [key: string]: number } const population: CityPopulation = { "Moskva": 12635466, "Sanktpēterburga": 5381033, }; -
Parameters<Type>: Atgriež funkcijas parametru tipu sarakstu (tuple).
function greet(name: string, age: number): string { return `Hello, ${name}! You are ${age}.`; } type GreetParams = Parameters<typeof greet>; // GreetParams būs [name: string, age: number] -
ReturnType<Type>: Atgriež funkcijas atgrieztās vērtības tipu.
function calculateSum(a: number, b: number): number { return a + b; } type SumResult = ReturnType<typeof calculateSum>; // SumResult būs number -
ConstructorParameters<Type>: Atgriež klases konstruktora parametru tipu sarakstu.
class MyClass { constructor(name: string, id: number) {} } type MyClassParams = ConstructorParameters<typeof MyClass>; // MyClassParams būs [name: string, id: number] -
InstanceType<Type>: Atgriež
Typeklases instancēta tipu.class AnotherClass { value: string; constructor(v: string) { this.value = v; } } type AnotherClassInstance = InstanceType<typeof AnotherClass>; // AnotherClassInstance būs AnotherClass
Izmantojot noderīgos tipus, kods kļūst drošāks, vieglāk uzturams un izteiksmīgāks, ļaujot efektīvi pārvaldīt sarežģītus datu tipus.