TS编译器遇到麻烦,抱怨字符串文字不满足应具有字符串的字符串文字联合类型。
type Payload = {
data: {
id: string;
hi: {
id: string;
};
}
}
type Filter<T extends Payload> = {
[K in keyof T]: T[K] extends { id: any } | undefined | null ? K : never;
};
type HasId<T extends Payload> = Filter<T>[keyof T];
const genericVersion = <T extends Payload> (something: T): HasId<T> => {
return 'data'; // error here
}
const staticVersion = <T extends Payload> (t: T): HasId<Payload> => {
return 'data';
}
const main = () => {
let d = genericVersion({ data: { id: '', hi: { id: '' } } });
}
我期望这两个功能都不会出错。