I am currently attempting to write a function with a complex type signature. It requires two dictionaries, args
and funcs
. The funcs
dictionary maps keys to functions that transform type A to B, while the args
dictionary maps a superset of those keys to either A if the key is in funcs
, or B otherwise. Essentially, if a function exists for a certain key, use it to map the value from the same key in the other dictionary; if not, simply use the value itself.
Here is what I have so far, but it doesn't seem to be functioning as expected:
function foo<Properties>(
args: { [P in keyof typeof funcs]: Parameters<typeof funcs[P]>[0] } & {
[P in Exclude<keyof Properties, keyof typeof funcs>]?: Properties[P];
},
funcs: { [P in keyof Properties]?: (attributes: any) => Properties[P] }
) {}
interface Bar {
x: number;
y: string;
}
foo<Bar>(
{
x: "a",
y: 1,
},
{
y: (a: string) => "a",
}
);
In this scenario, I would anticipate encountering two errors: one stating that x
should correspond to a number
in the first dictionary, and another indicating that y
should be a string
in the first dictionary. It appears that I may have misjudged TypeScript's capabilities - is achieving this intended behavior even possible?