function a<T extends SomeType>(arg: T): void {
// ...
}
данная конструкция означает, что обобщённый тип T должен иметь все свойства SomeTypefunction a<T = SomeType>(arg: T): void {
// ...
}
в данном случае мы задаём дефолтное значение для обобщённого типа T, то есть по умолчанию он будет равен SomeType, подобно параметрам по умолчанию в обычном javascriptfunction a<T extends SomeType = DefaultType>(arg: T): void {
// ...
}
сливая всё воедино, мы получаем, что обобщённый тип T должен быть расширением SomeType и по умолчанию равен DefaultType (который тоже должен являться расширением SomeType) type ValueOf<T> = T[keyof T];
const enum BlockType {
Paragpraph = 'PARAGRAPH',
Image = 'IMAGE',
List = 'LIST',
}
interface BodyTypes {
[BlockType.Paragpraph]: string;
[BlockType.Image]: File;
[BlockType.List]: string[];
}
interface IBlock<T extends BlockType> {
type: T;
body: BodyTypes[T];
}
type TBlock = ValueOf<{ [P in BlockType]: IBlock<P> }>;
// type TBlock = IBlock<BlockType.Paragpraph> | IBlock<BlockType.Image> | IBlock<BlockType.List>;
function x(b: TBlock) {
if (b.type === BlockType.Paragpraph) {
// b.body is string
} else if (b.type === BlockType.Image) {
// b.body is File
}
}
getStyles<T extends keyof CSSStyleDeclaration>(styles: Array<T>): Record<T, string> {
return styles.reduce((res, style) => {
res[style] = this.$nativeElement.style[style];
return res;
}, {} as Record<T, string>);
}
var InventoryCalculate = await this.getCalculateInventory(Offer, details);