我如何区分来自definitelyTyped的javascript声明typescript文件中的静态方法和实例方法?

时间:2016-08-27 13:43:52

标签: javascript typescript definitelytyped

如何从definitelyTyped的javascript声明打字稿文件中识别静态方法和实例方法以及其他方法?
比方说,exampleString.length是一个实例方法,而,String.fromCharCode()是一个静态方法。
我一直在寻找一种方法来编写一个可以识别另一个静态方法的脚本。用打字稿服务api说些什么。

1 个答案:

答案 0 :(得分:1)

评论太长了。

首先,String.prototype.length不是方法,而是值。如果使用class关键字定义构造函数,则可以通过检查方法是在构造函数中还是在实例中定义来检测静态和非静态方法。

class foo{
  bar(){}
  static baz(){}
}

var instance = new foo();

console.log(instance.constructor.bar); // undefined
console.log(instance.constructor.baz); // [Function: baz]
console.log(instance.bar); // [Function: bar]
console.log(instance.baz); // undefined

因此,您可以得出结论baz是静态的,bar是非静态方法。此外,String.length是个不错的示例:someString.length引用了String.prototype.length,但String.length引用了Function.prototype.length