I'm using a library (Constate) which contains types that I'd like to improve upon. It maps an array of functions to a different array of functions. Both arrays of functions share the same return types.
I.e. with objects, I can do:
const obj = {
a: arg => 123,
b: arg => 'str',
};
type T = { [k in keyof typeof obj]: () => ReturnType<typeof obj[k]> }
/*
T is {
a: () => number;
b: () => string;
}
*/
With arrays, this doesn't work:
const arr = [
arg => 123,
arg => 'str',
] as const;
type t2 = { [k in keyof typeof arr]: () => ReturnType<typeof arr[k]> }
/*
Type 'readonly [(_: any) => number, (_: any) => string][k]' does not satisfy the constraint '(...args: any) => any'.
Type '((_: any) => number) | ((_: any) => string) | 2 | (() => string) | (() => string) | { (...items: ConcatArray<((_: any) => number) | ((_: any) => string)>[]): (((_: any) => number) | ((_: any) => string))[]; (...items: (((_: any) => number) | ... 1 more ... | ConcatArray<...>)[]): (((_: any) => number) | ((_: any) =>...' is not assignable to type '(...args: any) => any'.
Type '2' is not assignable to type '(...args: any) => any'.
*/
I'm assuming Type '2'
in the error comes from .length
.
Is there a way to get only the array indices and use it with an index signature?