就表现力而言,我目前正在比较Google闭包编译器和流静态类型检查器。我知道如何在闭包中(而不是在流中)表示一个命名对象的集合,这些对象都具有相同的类型。在闭包中,可以使用注解。
/** @enum {function(number):number} */
const unaryFunctions = {
sin: Math.sin,
cos: Math.cos,
square: function(x) { return x*x; },
};有没有办法用Flow来做这样的事情?我想我可以使用一个ES6字典,而不是一个普通的对象,但是如果交叉编译到ES5,这会带来相当大的开销。我认为像这样的构造看起来非常惯用,所以我很惊讶我还没有在文档中找到匹配的类型描述。
发布于 2016-12-15 13:57:40
你可以这样做:
const unaryFunctions: { ['sin'|'cos'|'square']: (number) => number } = {
sin: Math.sin,
cos: Math.cos,
square: function(x) { return x*x; },
};如果不想复制密钥,可以重写一下:
const unaryFunctions_ = {
sin: Math.sin,
cos: Math.cos,
square: function(x) { return x*x; },
};
const unaryFunctions: { [$Keys<typeof unaryFunctions_>]: (number) => number } = unaryFunctions_;https://stackoverflow.com/questions/41165290
复制相似问题