從https://github.com/Microsoft/TypeScript/pull/3622:類型交點任何
超類型摺疊:一個& B等效於甲如果B是A的超類型
然而:
type a = string & any; // Resolves to any, not string!?
這十字路口解決任何。不是'任何'字符串的超類型嗎?所以不應該只是字符串,由於超類型崩潰?我錯過了什麼?
用例這裏是一樣的東西:
type PropertyMap = {
prop1: {
name: "somename";
required: any;
};
prop2: {
name: "someothername";
required: never;
}
}
type RequiredOnly = {
[P in keyof PropertyMap]: PropertyMap[P] & PropertyMap[P]["required"]
}
// RequiredOnly["prop2"] correctly inferred to be never, but we've
// lost the type info on prop1, since it is now an any (but should
// have been narrowed to it's original type).
任何幫助表示讚賞。