Asosiy matnga o'tish
ITIT Interview
TypeScript

Murakkab turlar

keyof, typeof, indexed access, mapped types, conditional types, infer, template literal types va satisfies operatori.

O'rganildi: 0 / 5
Progress

Bu nima?

TypeScript tur tizimi juda kuchli: turlardan yangi turlar hisoblash mumkin. Bu sahifadagi vositalar utility types va kutubxonalar turlarining asosi. Middle va senior intervyularda so'raladi.

keyof va typeof

interface User { id: number; name: string; }

type UserKeys = keyof User;          // 'id' | 'name'

const config = { port: 3000, host: 'localhost' };
type Config = typeof config;         // { port: number; host: string }
type ConfigKey = keyof typeof config; // 'port' | 'host'

TS'dagi typeof tur kontekstida qiymatning turini oladi (JS'dagi runtime typeof dan farqli).

Indexed access

type UserName = User['name'];                     // string
type Role = { roles: ('admin' | 'user')[] }['roles'][number];  // 'admin' | 'user'

const COLORS = ['red', 'green', 'blue'] as const;
type Color = (typeof COLORS)[number];             // 'red' | 'green' | 'blue'

Mapped types

Mavjud turning kalitlari bo'ylab yurib, yangi tur yasash:

type Flags<T> = {
  [K in keyof T]: boolean;
};
type UserFlags = Flags<User>;   // { id: boolean; name: boolean }

type Mutable<T> = {
  -readonly [K in keyof T]: T[K];   // readonly'ni olib tashlash
};

type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type UserGetters = Getters<User>;   // { getId: () => number; getName: () => string }

Conditional types

Tur darajasidagi if: T extends U ? X : Y.

type IsString<T> = T extends string ? true : false;
type A = IsString<'salom'>;   // true
type B = IsString<42>;        // false

Union bilan ishlaganda conditional tur har bir a'zoga alohida qo'llanadi (distributive):

type ToArray<T> = T extends any ? T[] : never;
type R = ToArray<string | number>;   // string[] | number[]

infer

Conditional tur ichida turning bir qismini "ushlab olish":

type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type ElementOf<T> = T extends (infer E)[] ? E : never;
type Unwrap<T> = T extends Promise<infer V> ? V : T;

type X = ElementOf<string[]>;          // string
type Y = Unwrap<Promise<number>>;      // number

Template literal types

type Size = 'sm' | 'md' | 'lg';
type Color = 'red' | 'blue';
type ClassName = `btn-${Size}-${Color}`;   // 'btn-sm-red' | 'btn-sm-blue' | ... (6 ta)

type EventName<T extends string> = `on${Capitalize<T>}`;
type ClickEvent = EventName<'click'>;      // 'onClick'

satisfies

Qiymat turga mosligini tekshiradi, lekin aniqroq chiqarilgan turni saqlab qoladi:

type Routes = Record<string, { path: string }>;

const routes = {
  home: { path: '/' },
  about: { path: '/about' },
} satisfies Routes;

routes.home.path;      // ok, avtoto'ldirish ishlaydi
routes.contact;        // Xato: bunday kalit yo'q

// ": Routes" deb yozilsa, routes.contact ham xato bermasdi (Record<string, ...>)

Intervyu savollari

Ushbu sahifada