-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed as not planned
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
🔎 Search Terms
"narrow tuple", "tuple inference"
🕗 Version & Regression Information
This is the behavior in every version I tried in the playground (v3.3, v4.5, v5.8, nightly), and I reviewed the FAQ about "tuple", "narrow", "inference".
⏯ Playground Link
💻 Code
interface T {}
const tlist: [T | null, T | null] = [null, null]
// OK if tlist is (T | null)[] but not for tuples, but I mean a tuple because length is fixed
function handle(tlist: [T, T]) {
tlist
}
if (tlist.every(t => t !== null)) {
handle(tlist)
}
if (tlist[0] !== null && tlist[1] !== null) {
handle(tlist)
}🙁 Actual behavior
handle(tlist) throws in the TS compiler:
Argument of type '[T | null, T | null] & T[]' is not assignable to parameter of type '[T, T]'.
Types of property '0' are incompatible.
Type 'T | null' is not assignable to type 'T'.
Type 'null' is not assignable to type 'T'.
🙂 Expected behavior
The condition in both if blocks should narrow tlist to the type [T, T], i.e. no error.
Additional information about the issue
It is OK if tlist is (T | null)[], but not for tuples. However, I really want to use a tuple here because the length of tlist is known and will never change.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created