4
为什么TypeScript中的'instanceof'会给我错误信息“'Foo'只引用一个类型,但是在这里被用作值。”?
我写了这段代码 interface Foo { abcdef: number; } let x: Foo | string; if (x instanceof Foo) { // ... } 但是TypeScript给了我这个错误: 'Foo' only refers to a type, but is being used as a value here. 为什么会这样呢?我以为instanceof可以检查我的值是否具有给定类型,但是TypeScript似乎不喜欢这样。