说我有以下字符串常量:流类型的常量字符串,并依赖类型
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
钥匙串?或者断言输入和输出具有相同的长度(例如换位密码功能)。或者说包含相同字符的排列? (用于洗牌)。
https://en.wikipedia.org/wiki/Dependent_type
如果你想'FOO'有类型''FOO''那么你需要声明它,否则它只是一个字符串。对于这些对象,你可以像输入错误那样输入:typeof FOO。虽然我不太清楚你在问你的问题。那么你最终会得到一个具有两个相同字符串值的属性的对象。 – loganfsmyth