Home > front end >  Array element type assertion problem in TypeScript
Array element type assertion problem in TypeScript

Time:07-06

type Plan<T> = [T[], ...T[]];

I declared a type named Plan, which includes a repetitive plan in index 0 and in the rest what to execute initially.

example) const life: Plan<string> = [ ["getUp", "work", "sleep"], "birth", "grow" ]

And I tried to define the following function:

function parsePlan<T>(plan: Plan<T>, index: number): T {
    if(index < 1) throw Error();

    return index < plan.length
    ?plan[index]
    :plan[0][(index-plan.length)%(plan[0].length)]
}

but It says there is a problem that this would return T | T[].

If index < 1 it will throw Error() and that makes it impossible to return T[].

Why does this error appear, and how can I fix this error?

Thanks a lot for your help.

CodePudding user response:

Type narrowing doesn't work in all cases, as you expect.

It does work on a single variable though, when you add type guards or type predicates to narrow down.

This will work:

type Plan<T> = [T[], ...T[]];

function parsePlan<T>(plan: Plan<T>, index: number): T {
    const item = plan[index]

    if (Array.isArray(item)) throw Error();

    return index < plan.length ? item : plan[0][(index-plan.length)%(plan[0].length)]
}

Also playground here.

  • Related