Sobes.tech
Middle

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 no Type.

    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 no Type.

    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 Type elementus, kas var tikt piešķirti ExcludedUnion.

    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 Type elementus, kas var tikt piešķirti Union.

    type AllAnimals = 'dog' | 'cat' | 'fish' | 'bird';
    type PetAnimals = Extract<AllAnimals, 'dog' | 'cat'>;
    // PetAnimals būs 'dog' | 'cat'
    
  • NonNullable<Type>: Izveido tipu, izslēdzot no Type null un undefined.

    type MaybeString = string | null | undefined;
    type NotNullableString = NonNullable<MaybeString>;
    // NotNullableString būs string
    
  • Record<Keys, Type>: Izveido objektu tipu, kura īpašības ir no Keys un vērtības ir Type.

    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ž Type klases 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.