TypeScript 实例不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45964008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript instanceof not working
提问by AnimaSola
I'm having issues using the instanceof operator and it doesn't seem to work. Here is a part of my code:
我在使用 instanceof 运算符时遇到问题,但它似乎不起作用。这是我的代码的一部分:
const results = _.map(items, function(item: Goal|Note|Task, index: number) {
let result = {};
if (item instanceof Goal) {
result = { id: index, title: item.name };
} else if (item instanceof Note) {
result = { id: index, title: item.content.text };
} else if (item instanceof Task) {
result = { id: index, title: item.name };
}
console.log(item);
console.log(item instanceof Goal);
console.log(item instanceof Note);
console.log(item instanceof Task);
return result;
});
All of my logs say false, here is what the console looks like:
我所有的日志都说错误,这是控制台的样子:
None of them match, despite being explicit that only the 3 types would be possible. You could also see the object itself with a typename of Goal, so I don't get why it doesn't match with instanceof Goal.
尽管明确表示只有 3 种类型是可能的,但它们都不匹配。您还可以看到类型名称为 Goal 的对象本身,所以我不明白为什么它与 instanceof Goal 不匹配。
Any ideas?
有任何想法吗?
回答by Gopikrishna S
instanceof
will return true only if it matches the function or class from which it was constructed. The item
here is a plain Object
.
instanceof
仅当它与构造它的函数或类匹配时才返回 true。在item
这里是一个平原Object
。
const a = { a: 1 } // plain object
console.log(a);
// {a:1} <-- the constructor type is empty
// a: 1
// __proto__: Object <-- inherited from
a instanceof A // false because it is a plain object
a instanceof Object // true because all object are inherited from Object
If it is constructed using a constructor function or a class, then instanceof will work as expected:
如果它是使用构造函数或类构造的,则 instanceof 将按预期工作:
function A(a) {
this.a = a;
}
const a = new A(1); // create new "instance of" A
console.log(a);
// A {a:1} <-- the constructor type is `A`
a instanceof A // true because it is constructed from A
a instanceof Object // true
If Goal
is an Interface
it will only check the structure of the object not its type. If Goal
is a constructor then it should return true for instanceof
checks.
如果Goal
是 anInterface
它只会检查对象的结构而不是它的类型。如果Goal
是构造函数,则它应该返回 true 以进行instanceof
检查。
Try something like:
尝试类似:
// interface Goal {...}
class Goal {...} // you will have to change the way it works.
items = [
new Goal()
];
回答by AFD
You can also use type guards to your advantage:
您还可以使用类型保护来发挥自己的优势:
https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html
https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html
https://www.typescriptlang.org/docs/handbook/advanced-types.html
https://www.typescriptlang.org/docs/handbook/advanced-types.html
For instance, if you use a literal type guard to your class:
例如,如果您对类使用文字类型保护:
class Goal {
type: 'goal'
...
}
then the check is as simple as :
那么检查很简单:
if (item.type === 'goal') {
}
Or you could write your own type guards:
或者你可以编写自己的类型保护:
function isNote(arg: any): arg is Note {
// because only your Note class has "content" property?
return arg.content !== undefined;
}
if (isNote(item)) {
result = { id: index, title: item.content.text };
}
回答by Lucia
Try to instantiate the object with a constructor. It happened to me the same because I was manually mocking the object for testing purposes. If you create the item like following example, it should work:
尝试使用构造函数实例化对象。我也遇到了同样的情况,因为我手动模拟了对象以进行测试。如果您像以下示例一样创建项目,它应该可以工作:
item: Goal = new Goal(*item values*)