This piece works in TypeScript 2.6:
function resolver<Key extends keyof HashType>(a: Command<Key>): HashType[Key]['out'] {
return handlers[a.kind](a);
}
const handlers: {[k in keyof HashType]: (arg: Command<k>) => HashType[k]['out']} = {
a: arg => 1,
b: arg => ''
};
type Command<Key extends keyof HashType> = HashType[Key]['in'] & { kind: Key }
type HashType = {
a: { in: { someString: string }, out: number }
b: { in: { someNumber: number }, out: string }
}
However, since 2.7
, it fails with:
TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((arg: Command<"a">) => number) | ((arg: Command<"b">) => string)' has no compatible call signatures.