Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript infer about array[index]

Tags:

typescript

I expected result is number | never[]. But actually number | never[] | undefined.

type T1 = (number | undefined)[]

const myFunc = (arr: T1, index: number) => {
    const result = arr[index] === undefined ? [] : arr[index]
    return result // number | never[] | undefined
}

Why does it behave like this ?

like image 358
green Avatar asked Dec 13 '25 22:12

green


1 Answers

TypeScript uses flow analysis to narrow down types of variables. However, it seems unable to narrow down arr just by checking that one of the elements is undefined:

type T1 = (number | undefined)[]
const myFunc = (arr: T1, index: number) => {
    if (arr[index] === undefined) {
        return []; // never[]
    }
    //the type of `arr` is still considered T1
    let temp = arr[index]; //number | undefined 
    return temp;
}

Actually, I'm not sure how else should the type of arr be represented as after the if block (how would you represent "an array of number|undefined, except that this particular dynamic index is not undefined"?). It still makes some sense to simply fall back to T1.

Your code uses ternary operator, but it acts similarly to my code above.

However, TypeScript can narrow down the types for a result variable when the variable is used in a direct comparison in a flow control statement:

type T1 = (number | undefined)[]
const myFunc = (arr: T1, index: number) => {
    const result = arr[index];
    if (result === undefined) {
        return []; // never[]
    }
    //the type of `result` is correctly determined as number, not undefined
    return result;
}

I think arr and result (variables) may have their types narrowed down by control flow, but arr[index] (an expression, not just a variable) will just be computed from the types of arr and index at that point.

like image 185
qrsngky Avatar answered Dec 16 '25 22:12

qrsngky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!