0
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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.