說我有以下字符串常量:流類型的常量字符串,並依賴類型
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