I want to create a tagged scheme of input and output types for function, so that having a tag passed into a function it would infer the input and output types.
Here is an example:
type Aa = {
tag: 'a',
a: 2
}
type Ab = {
tag: 'b',
a: 3
}
type TInputMap = {
'a': Aa,
'b': Ab
}
type TResultMap = {
'a': string,
'b': number
}
type A = Ab | Aa;
const fn = <TAction extends keyof TInputMap>(type: TAction, action: TInputMap[TAction]): TResultMap[TAction] => {
if (type === 'a') {
action.a // expected to be 2, but inferred as 2 | 3
}
}
Is it at all possible? Maybe using conditional types?
Thanks.