在为构造函数的参数使用接口时,是否有可能得到更严格的编译时间检查?默认行为似乎过于宽松。
例如,给定以下类:
// @flow
'use strict';
import type { Bar } from './bar';
export default class Foo {
_bar: Bar;
_name: string;
constructor (bar: Bar, name: string) {
this._bar = bar;
this._name = name;
}
}
以及在另一个地方定义的以下接口:
// @flow
'use strict';
export interface Bar {
doSomething(someArg: string);
}
如果我用某种原始类型创建Foo的无效实例,我将得到一个错误:
// In any of these flowtype checking works and fails because
// it knows those things are not Bar.
new Foo('bar', 'someName');
new Foo(1, 'someName');
new Foo({}, 'someName');
但如果我做这样的傻事:
new Foo(new Function(), 'someName');
flowtype对此非常满意,而且这种类型违背了在一开始就定义接口的目的。如果我可以传入任何类型的实例对象,而for类型没有看到传入的内容与接口不匹配,那么它应该抛出一个错误,就像对{}
那样。
有什么配置我需要改变或者我做错了什么吗?
编辑:
我认为这可能是一个错误,并已提交了一个问题。
发布于 2016-11-11 22:27:06
https://stackoverflow.com/questions/40554798
复制相似问题