So this works beautifully, using @next version of Typescript
function fork<
Paths extends Partial<Paths>,
OutputType extends keyof Paths
> (callback: () => OutputType, paths: Paths) {
const result = callback()
const path: () => ReturnType<Paths[OutputType]> = paths[result]
const pathResult = path()
return pathResult
}
const myResult = fork(() => 'bar', {
foo: () => 'bip',
bar: () => 123
})
Based on the returned string of first callback, the myResult gets correct typing.
However, when I do this small change:
function fork<
Paths extends Partial<Paths>,
OutputType extends keyof Paths
> (callback: () => OutputType, paths: Paths) {
const result = callback()
const path: (p: string) => ReturnType<Paths[OutputType]> = paths[result]
const pathResult = path('foo')
return pathResult
}
const bah = fork(() => 'bar', {
foo: (p) => 'bip',
bar: (p) => 123
})
Basically just adding an argument, the whole thing breaks with no good error message.
To look at this just paste the code into VS Code using the latest 2.8 (@next) version of Typescript.
ReturnType<Paths[OutputType]