#每天一个知识点#
export declare class TagProtector<T extends string> {
protected __tag__: T;
}

export type Nominal<T, U extends string> = T & TagProtector<U>;


export type USD = Nominal<number, 'USD'>;

export type CNY = Nominal<number, 'CNY'>;


const CNYCount = 100 as CNY;

const USDCount = 100 as USD;

function addCNY(source: CNY, input: CNY) {
return (source + input) as CNY;
}

addCNY(CNYCount, CNYCount);

// 报错了!
addCNY(CNYCount, USDCount);

没太懂怎么实现标准化类型系统,使用联合类型为啥
展开
评论