You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tuple type not inferred when calling generic function #6310
Tuple types aren't always inferred when calling generic functions. For example:
function identity<A>(value: A): A {
return value
}
// works
const [a1, b1]: [number, string] = [1, 'a']
// works
const [a2, b2]: [number, string] = identity<[number, string]>([1, 'a'])
// breaks
const [a3, b3]: [number, string] = identity([1, 'a'])
Output:
foo.ts(12,7): error TS2322: Type '(number | string)[]' is not assignable to type '[number, string]'.
Property '0' is missing in type '(number | string)[]'.
This is because the array expression needs to be contextually typed by a tuple-like type so that we can infer your type argument as a tuple. You've enforced that when you explicitly gave a type argument, but not in the last example.
One way you could achieve what you want is to add a tuple-like constraint for your type parameter:
Activity
DanielRosenwasser commentedon Jan 2, 2016
This is because the array expression needs to be contextually typed by a tuple-like type so that we can infer your type argument as a tuple. You've enforced that when you explicitly gave a type argument, but not in the last example.
One way you could achieve what you want is to add a tuple-like constraint for your type parameter:
zpdDG4gta8XKpMCd commentedon Jan 4, 2016
that's the most interesting
identifyfunction I've seen so far