Povězte mi o utilitárních typech v TypeScriptu.
sobes.tech AI
Odpověď od AI
Užitečné typy v TypeScriptu jsou vestavěné typy, které umožňují převádět stávající datové typy na nové typy prováděním různých užitečných operací. Pomáhají zvýšit flexibilitu a znovupoužitelnost typů, snižují redundanci kódu.
Hlavní užitečné typy:
-
Partial<Type>: Vytváří nový typ, ve kterém jsou všechny vlastnosti
Typevolitelné.interface User { id: number; name: string; age: number; } type PartialUser = Partial<User>; // PartialUser bude { id?: number; name?: string; age?: number; } -
Readonly<Type>: Vytváří nový typ, ve kterém jsou všechny vlastnosti
Typepouze pro čtení.interface Point { x: number; y: number; } type ReadonlyPoint = Readonly<Point>; // ReadonlyPoint bude { readonly x: number; readonly y: number; } -
Pick<Type, Keys>: Vytváří typ výběrem dané sady vlastností
KeyszType.interface Product { id: number; name: string; price: number; description: string; } type ProductSummary = Pick<Product, 'id' | 'name' | 'price'>; // ProductSummary bude { id: number; name: string; price: number; } -
Omit<Type, Keys>: Vytváří typ vyloučením dané sady vlastností
KeyszType.interface Order { id: number; productId: number; quantity: number; timestamp: string; } type OrderDetails = Omit<Order, 'timestamp'>; // OrderDetails bude { id: number; productId: number; quantity: number; } -
Exclude<Type, ExcludedUnion>: Vytváří typ vyloučením z
Typetěch prvků, které lze přiřaditExcludedUnion. Používá se pro unie (union types).type Colors = 'red' | 'green' | 'blue' | 'yellow'; type PrimaryColors = Exclude<Colors, 'yellow'>; // PrimaryColors bude 'red' | 'green' | 'blue' -
Extract<Type, Union>: Vytváří typ výběrem z
Typetěch prvků, které lze přiřaditUnion. Používá se pro unie.type AllAnimals = 'dog' | 'cat' | 'fish' | 'bird'; type PetAnimals = Extract<AllAnimals, 'dog' | 'cat'>; // PetAnimals bude 'dog' | 'cat' -
NonNullable<Type>: Vytváří typ vyloučením hodnot
nullaundefinedzType.type MaybeString = string | null | undefined; type NotNullableString = NonNullable<MaybeString>; // NotNullableString bude string -
Record<Keys, Type>: Vytváří typ objektu, jehož vlastnosti mají klíče z
Keysa hodnoty typuType.type CityPopulation = Record<string, number>; // CityPopulation bude { [key: string]: number } const population: CityPopulation = { "Moskva": 12635466, "Sankt-Petěrburg": 5381033, }; -
Parameters<Type>: Vrací tuple typů parametrů funkce
Type.function greet(name: string, age: number): string { return `Hello, ${name}! You are ${age}.`; } type GreetParams = Parameters<typeof greet>; // GreetParams bude [name: string, age: number] -
ReturnType<Type>: Vrací typ návratové hodnoty funkce
Type.function calculateSum(a: number, b: number): number { return a + b; } type SumResult = ReturnType<typeof calculateSum>; // SumResult bude number -
ConstructorParameters<Type>: Vrací tuple nebo pole typů parametrů konstruktoru třídy
Type.class MyClass { constructor(name: string, id: number) {} } type MyClassParams = ConstructorParameters<typeof MyClass>; // MyClassParams bude [name: string, id: number] -
InstanceType<Type>: Vrací typ instance třídy
Type.class AnotherClass { value: string; constructor(v: string) { this.value = v; } } type AnotherClassInstance = InstanceType<typeof AnotherClass>; // AnotherClassInstance bude AnotherClass
Použití užitečných typů činí kód bezpečnějším, udržovatelnějším a výstižnějším, umožňuje efektivní správu složitých datových typů.