简体   繁体   中英

Array element type assertion problem in TypeScript

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[] 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM