Here we can call getValue().b to get computed value:
function foo<
Computed extends {
[key: string]: (param1: string, param2: number) => any;
},
>(options: {
computed?: Computed;
callback?: (
getValue: () => {
[K in keyof Computed]: ReturnType<Computed[K]>;
},
) => void;
}) {
return options;
}
foo({
computed: {
b: () => 2,
},
callback: (getValue) => {
getValue().b; // work!
},
});
But it get wrong when i want to define callback parameter type in each computed value:
function foo<
Computed extends {
[key: string]: (param1: string, param2: number) => any;
},
>(options: {
computed?: Computed;
callback?: (
getValue: () => {
[K in keyof Computed]: ReturnType<Computed[K]>;
},
) => void;
}) {
return options;
}
foo({
computed: {
b: (x, y) => x + y, // add x, y here!
},
callback: (getValue) => {
getValue().b; // not work! the type of b is any!
},
});
PS: this demo works, but arguments to function foo is not what i want.
function foo<
Computed extends {
[key: string]: (param1: string, param2: number) => any;
},
>(
computed?: Computed,
callback?: (
getValue: () => {
[K in keyof Computed]: ReturnType<Computed[K]>;
},
) => void,
) {}
foo(
{
b: (x, y) => x + y,
},
(getValue) => {
getValue().b; // work!
},
);
So, my question is, why the last demo worked, but second not?