I have an odd error where the code below works, however the compiler is giving me an error.
I believe C
in ...args: C
in both spots is throwing
: A rest parameter must be of an array type. (2370)
: A rest parameter must be of an array type. (2370)
: Type '<A extends string, B extends string, C extends any[], D>(A: A, B: B, fn: (...args: C) => D) => { ...' is not assignable to type 'MakeFromTo'.
Type '{ [x: string]: { to: { [x: string]: (...args: C) => D; }; }; }' is not assignable to type 'Record<A, { to: Record<B, (...args: C) => D>; }>'. (2322)
Here is the code:
type MethodResult<TFrom extends string, TTo extends string, C extends any[], D> = Record<TFrom, {
to: Record<TTo, (...args: C) => D>;
}>;
type MakeFromTo = <A extends string, B extends string, C extends any[], D> (A: A, B: B, fn: (...args: C) => D) => MethodResult<A, B, C, D>;
const makeFromTo: MakeFromTo = (A, B, fn) => ({ [A]: { to: { [B]: fn } } });
Using typescript 2.6.2