Here is the code snippet that I am working with:
export type FooParams = {
foo1: { x: number };
foo2: { y: string };
};
export type FooKey = keyof FooParams; // or export type FooKey = "foo1" | "foo2";
export interface FooAction<T extends FooKey> {
execute: (params: FooParams[T]) => void;
}
const foo1Action: FooAction<"foo1"> = {
execute: (params) => {
console.log(params.x);
},
};
const foo2Action: FooAction<"foo2"> = {
execute: (params) => {
console.log(params.y);
},
};
export const fooActions: Record<FooKey, FooAction<FooKey>> = {
foo1: foo1Action,
foo2: foo2Action,
};
I am facing an issue where I cannot strongly type the variable fooActions
to ensure a FooAction
for every FooKey
. The error message in this example points out the following problem.
Type 'FooAction<"foo1">' is not assignable to type 'FooAction<keyof FooParams>'.
Type 'keyof FooParams' is not assignable to type '"foo1"'.
Type '"foo2"' is not assignable to type '"foo1"'.ts(2322)
If you have any suggestions on how to correctly declare the type of fooActions
, please let me know!