Dev Highlights

Доки по разработке

This project is maintained by teniryte

Извлечение типов через infer

Принципы

Пример

type ArrayUnpacker<T> = T extends Array<infer U> ? U : never;
type PromiseUnpacker<T> = T extends Promise<infer U> ? U : never;

class Box<T> {
  constructor(private readonly value: T) {}
}
type BoxUnpacker<T> = T extends Box<infer U> ? U : never;

const stringArray = ['this', 'is', 'cool'];
type Element = ArrayUnpacker<typeof stringArray>; // string

Практика