参观这个游乐场:Playground
export interface IProduct {
productId: number;
}
export interface ICompany {
companyId: number;
}
function click(type: 'product', entity: IProduct): string;
function click(type: 'company', entity: ICompany): string;
function click<T>(type: 'product' | 'company', entity: T) {
if (type === 'product') {
return 'product ' + entity.productId;
} else if (type === 'company') {
return 'company ' + entity.companyId;
}
throw new Error('wrong input');
}
click('product', { productId: 123 });
如何使打字稿理解,如果第一个if的值为true,那么第二个参数为IProduct
?
答案 0 :(得分:2)
答案 1 :(得分:0)
function click(type: 'product', entity: IProduct): string;
function click(type: 'company', entity: ICompany): string;
function click<T extends IProduct | ICompany>(type: 'product' | 'company', entity: T) {
if (type === 'product' && assertType(type, entity)) {
return 'product ' + entity.productId;
} else if (type === 'company' && assertType(type, entity)) {
return 'company ' + entity.companyId;
}
throw new Error('wrong input');
}
function assertType(type: 'product', entity: IProduct | ICompany): entity is IProduct;
function assertType(type: 'company', entity: IProduct | ICompany): entity is ICompany;
function assertType(type: 'product' | 'company', entity: IProduct | ICompany): entity is IProduct | ICompany {
return entity && type === "product" || type === "company";
}
function click(entity: IProduct | ICompany) {
// here: (parameter) entity: IProduct | ICompany
if ("productId" in entity) {
// here: (parameter) entity: IProduct
return `product ${entity.productId}`;
} else if ("companyId" in entity) {
// here: (parameter) entity: ICompany
return `company ${entity.companyId}`;
}
throw new Error('wrong input');
}