TypeScript 中的类类型检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12789231/
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
Class type check in TypeScript
提问by Mark Knol
In ActionScript, it is possible to check the type at run-time using the is operator:
在 ActionScript 中,可以使用is 运算符在运行时检查类型:
var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite); // true
trace(mySprite is DisplayObject);// true
trace(mySprite is IEventDispatcher); // true
Is it possible to detect if a variable (extends or) is a certain class or interface with TypeScript?
是否可以使用 TypeScript 检测变量(扩展或)是否是某个类或接口?
I couldn't find anything about it in the language specs. It should be there when working with classes/interfaces.
我在语言规范中找不到任何关于它的信息。在使用类/接口时它应该在那里。
回答by Zeta
4.19.4 The instanceof operator
The
instanceof
operator requires the left operand to be of type Any, an object type, or a type parameter type, and the right operand to be of type Any or a subtype of the 'Function' interface type. The result is always of the Boolean primitive type.
4.19.4 instanceof 运算符
在
instanceof
操作者需要的左操作数是类型的任何,对象类型,或类型参数类型和右操作数是任何类型或“功能”接口类型的子类型的。结果总是布尔基元类型。
So you could use
所以你可以使用
mySprite instanceof Sprite;
Note that this operator is also in ActionScript but it shouldn't be used there anymore:
请注意,此运算符也在 ActionScript 中,但不应再在那里使用:
The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or expression is a member of a given data type. In previous versions of ActionScript, the instanceof operator provided this functionality, but in ActionScript 3.0 the instanceof operator should not be used to test for data type membership. The is operator should be used instead of the instanceof operator for manual type checking, because the expression x instanceof y merely checks the prototype chain of x for the existence of y (and in ActionScript 3.0, the prototype chain does not provide a complete picture of the inheritance hierarchy).
is 运算符是 ActionScript 3.0 的新增功能,它允许您测试变量或表达式是否是给定数据类型的成员。在以前版本的 ActionScript 中, instanceof 运算符提供了此功能,但在 ActionScript 3.0 中,instanceof 运算符不应用于测试数据类型成员资格。应该使用 is 运算符而不是 instanceof 运算符来进行手动类型检查,因为表达式 x instanceof y 仅检查 x 的原型链是否存在 y(而在 ActionScript 3.0 中,原型链不提供继承层次结构)。
TypeScript's instanceof
shares the same problems. As it is a language which is still in its development I recommend you to state a proposal of such facility.
TypeScript 也instanceof
有同样的问题。由于它是一种仍在开发中的语言,我建议您提出有关此类设施的建议。
See also:
也可以看看:
- MDN: instanceof
- MDN:实例
回答by Gilad S
TypeScript have a way of validating the type of a variable in runtime. You can add a validating function that returns a type predicate. So you can call this function inside an if statement, and be sure that all the code inside that block is safe to use as the type you think it is.
TypeScript 有一种在运行时验证变量类型的方法。您可以添加一个返回类型谓词的验证函数。因此,您可以在 if 语句中调用此函数,并确保该块中的所有代码都可以安全地用作您认为的类型。
Example from the TypeScript docs:
TypeScript 文档中的示例:
function isFish(pet: Fish | Bird): pet is Fish {
return (<Fish>pet).swim !== undefined;
}
// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
pet.swim();
}
else {
pet.fly();
}
See more at: https://www.typescriptlang.org/docs/handbook/advanced-types.html
查看更多信息:https: //www.typescriptlang.org/docs/handbook/advanced-types.html
回答by Willem van der Veen
You can use the instanceof
operator for this. From MDN:
您可以instanceof
为此使用运算符。来自 MDN:
The instanceof operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object.
instanceof 运算符测试构造函数的原型属性是否出现在对象原型链中的任何位置。
If you don't know what prototypes and prototype chains are I highly recommend looking it up. Also here is a JS (TS works similar in this respect) example which might clarify the concept:
如果您不知道什么是原型和原型链,我强烈建议您查找一下。这里还有一个 JS(TS 在这方面的工作类似)示例,它可能会阐明这个概念:
class Animal {
name;
constructor(name) {
this.name = name;
}
}
const animal = new Animal('fluffy');
// true because Animal in on the prototype chain of animal
console.log(animal instanceof Animal); // true
// Proof that Animal is on the prototype chain
console.log(Object.getPrototypeOf(animal) === Animal.prototype); // true
// true because Object in on the prototype chain of animal
console.log(animal instanceof Object);
// Proof that Object is on the prototype chain
console.log(Object.getPrototypeOf(Animal.prototype) === Object.prototype); // true
console.log(animal instanceof Function); // false, Function not on prototype chain
The prototype chain in this example is:
本例中的原型链为:
animal > Animal.prototype > Object.prototype
动物 > Animal.prototype > Object.prototype