【发布时间】:2020-05-21 13:12:37
【问题描述】:
以下抽象的 TS 场景:
interface EmotionsOkay {
emotion: string;
okay: "yap";
}
interface EmotionsNotOkay {
emotion: string;
}
type UndetereminedEmotion = EmotionsOkay | EmotionsNotOkay;
const areYouOkay = (test: UndetereminedEmotion) => {
console.log(test.okay ? "happy :D" : "sad D:");
};
在控制台记录 test.okay 时抛出 TypeScript 错误,因为它显然不存在。
Property `okay` does not exist on type `UndetereminedEmotion`.
即使它很可能存在,但如果传递给方法的测试是EmotionsOkay 类型。
为什么会这样?
【问题讨论】:
标签: typescript typescript-typings