使用TypeScript检查类类型

使用TypeScript检查类类型

本文介绍了使用TypeScript检查类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对感到非常兴奋,所以我开始玩它。作为一个Actionscript开发人员,它使Javascript变得不那么难了。

I'm very excited about TypeScript, so I started to play with it. As an Actionscript developer, it makes Javascript less hard.

但是,在Actionscript中,可以使用:

However, in Actionscript it is possible to check the type at run-time using the is operator:

var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite); // true
trace(mySprite is DisplayObject);// true
trace(mySprite is IEventDispatcher); // true

是否可以检测变量(extends或)是否是某个类或接口用TypeScript?我在语言规范中找不到任何关于它的东西,在使用类/接口时它应该存在。

Is it possible to detect if a variable (extends or) is a certain class or interface with TypeScript? I couldn't find anything about it in the language specs, it should be there when working with classes/interfaces.

推荐答案

所以你可以使用

mySprite instanceof Sprite;

请注意,此运算符也在ActionScript中,但不应再在那里使用:

Note that this operator is also in ActionScript but it shouldn't be used there anymore:

TypeScript的 instanceof 会遇到同样的问题。由于这是一种仍处于开发阶段的语言,我建议您说明此类设施的提案。

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.

参见:


  • MDN:

  • MDN: instanceof

这篇关于使用TypeScript检查类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:38