我有很多遗留的ES5代码,它们不使用class关键字来定义“类”。我想注释和做类型检查与流。这有可能吗?
我只找到了使用class关键字:_的例子
发布于 2016-06-10 09:19:04
由于ES5 "classes“只是函数,所以您肯定可以为许多相同的检查添加类型注释。
例如,这类流的ES6类描述
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!';
};虽然您确实错过了额外的类属性,检查名称。
与我怀疑的问题更相关的是,通过使用构造函数名称作为类型注释,您似乎可以检查其他变量/参数等,以确定它们是否与ES5“类”匹配:
https://flowtype.org/docs/objects.html#constructor-functions-and-prototype-objects
编辑:是的,我只是天真地添加了这样的注释:
this.name: string = name;但是that类型目前不喜欢这样。除非有人知道得更清楚,我想唯一的解决办法是
const tmpName: string = name;
this.name = tmpName;虽然这显然不是很好。
https://stackoverflow.com/questions/37713201
复制相似问题