Parameters<> utility type cannot infer types when using generic type as index

Viewed 54

Consider this example, also below:

enum Options {
    one,
    two,
    three
}

const dictionary = {
    [Options.one]: (a: string, b: boolean) => { console.log({ a: a, b: b }) },
    [Options.two]: (c: number) => { console.log(c) },
    [Options.three]: () => { },
} as const

function complicated<T extends Options>(option: T, ...args: Parameters<typeof dictionary[T]>) {
    const fn = dictionary[option]
    type b = typeof dictionary[T]
    fn(...args) // This error shows for Typescript but running gives no errors.
}

When creating a type statically like type One = Parameters<typeof dictionary[Options.one]>, the type is correct, as [a: string, b: boolean].

But using the generic parameter above with Parameters<typeof dictionary[T]>, the fn variable shows it is a (arg0: never, b: boolean) => void. Calling fn(...arg) is type checked to have error, when executing the script works fine.

Why is this and what should be done to eliminate the error?

Edit: This is currently an open issue on Github #49802, also referred to in #49700

0 Answers
Related