你可以使用Facebook的stream注释未使用ES6类关键字定义的类吗?

我有很多遗留的ES5代码,不使用类关键字来定义“类”。 我想注释并使用Flow进行types检查。 那可能吗?

我只find使用class关键字的示例: https : //flowtype.org/docs/classes.html#_

由于ES5“类”只是function,你可以肯定地添加types注释许多相同的检查

例如这个ES6 Class用Flow定义

class Hello { name: string; constructor(name: string) { this.name = name; } hello(): string { return 'Hello ' + this.name + '!'; } static sayHelloAll(): string { return 'Hello everyone!'; } } 

可以这样写:

 function Hello(name: string) { this.name = name; } Hello.prototype.hello = function hello(): string { return 'Hello ' + this.name + '!'; }; Hello.sayHelloAll = function (): string { return 'Hello everyone!'; }; 

虽然你错过了名字额外的类属性检查。

更相关的你的问题,我怀疑,你似乎可以检查其他variables/参数等,看看他们是否符合你的ES5“类”使用构造函数名称作为types注释:

https://flowtype.org/docs/objects.html#constructor-functions-and-prototype-objects

编辑:是啊,我只是尝试天真地添加像这样的注释:

 this.name: string = name; 

但stream量types目前不是这样的。 除非有人知道更好,我猜唯一的解决方法是类似的

 const tmpName: string = name; this.name = tmpName; 

虽然这显然不是很好。