interface IdLabel {
id: number;
}
interface NameLabel {
name: string;
}
type NameOrId<T extends number | string> = T extends number
? IdLabel
: NameLabel;
let idLabel: IdLabel = {
id: 123,
};
let nameLabel: NameLabel = {
name: "123",
};
function createLabel<T extends number | string>(arg: T): NameOrId<T> {
return arg === "string" ? nameLabel : idLabel;
}
如何实现这个createLabel
函数体,按我的理解这个实参返回NameLabel
或IdLabel
类型的值
按我的写法,提示Type 'IdLabel | NameLabel' is not assignable to type 'NameOrId<T>'.
有没有大佬解惑下,感恩
这种情况用Function Overloads