【发布时间】:2017-06-30 22:11:53
【问题描述】:
假设我有以下常量字符串:
export default const FOO = 'FOO'
假设我将它导入到流注释文件中,如下所示:
import FOO from '../consts/Foo'
然后我有一个函数:
const example = (foo : string) : {| type: FOO, foo: string |} => {
return {type: FOO, foo: foo}
}
这不会与以下内容进行类型检查:
6: const example = (foo : string) : {| type: FOO, foo: string |}=> {
^^^^^^^^^^^^^^ string. Ineligible value used in/as type annotation (did you forget 'typeof'?)
6: const example = (foo : string) : {| type: FOO, foo: string |}=> {
^^^^^^^^^^^^^^ FOO
所以我的问题是:
1) 是否可以在流类型中使用常量,如何重现这种行为?
2) 是否可以在流中做依赖类型?例如,我是否可以通过类型编码返回的字符串必须与传递给example 函数的字符串相同?
编辑:对第 2 部分的说明:是否有可能以某种方式表明传递给 example 函数的 foo 参数实际上与返回对象中 foo 键处的字符串相同?或者断言输入和输出具有相同的长度(例如移位密码函数)。或者说包含相同字符的排列? (随机播放)。
【问题讨论】:
-
如果您希望
FOO具有'FOO'类型,那么您需要声明它,否则它只是一个字符串。对于对象,您可以像错误所说的那样执行type: typeof FOO。不过,我不太确定您在 2) 中要问什么。然后你会得到一个对象,它有两个属性相同的字符串值。
标签: javascript types ecmascript-6 flowtype dependent-type