Try flow link.限定された多型を改良することは可能ですか?ここで
は、私はそれを期待するように動作しません。簡単な有界多型の例です:
// @flow
function thisBreaks<T: 'a' | 'b'>(x: T): T {
if (x === 'a') {
return 'a'
} else {
return 'b'
}
}
function thisWorks<T: 'a' | 'b'>(x: T): T {
return x
}
const a = 'a'
const aPrime: 'a' = thisWorks(a)
const b = 'b'
const bPrime: 'b' = thisWorks(b)
5: return 'a'
^string. This type is incompatible with the expected return type of
3: function thisBreaks<T: 'a' | 'b'>(x: T): T {
^some incompatible instantiation of `T`
7: return 'b'
^string. This type is incompatible with the expected return type of
3: function thisBreaks<T: 'a' | 'b'>(x: T): T {
^some incompatible instantiation of `T`
私は例えば以来、仕事をする最初の例を期待しているだろうx === 'a'
小切手でT
〜'a'
の権利を得ることができますか?
を返すことができますよりも、それは可能ではないですが、あなたが機能して何をしたいかを達成することができるかもしれません交差点。私はあなたの関数のための作業の種類を作成しました:(https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjOA7AzgFzCjjgC4wAKCgD3IHIBDOgSjAF4A+MRl1MfgQIBklGvQBGLdlzqTmqNqOrSwytuu5MwAfk10wEluQbYAnvMw4CYcQwBO9LYuJwKPeVjyFbAL0PsiEjc5Sy8iBgBLGFxHfWcg2V4gA)[フローリンクをお試しください] – Andy