Consider having an Object Type named Options
, which consists of two keys:
strategy
: a function that takes one parameter of an unknown typeparameter
: the same type as the first argument ofstrategy
The desired functionality is as follows:
type FooParameter = { foo: string }
type BarParameter = { bar: string }
const fooOptions: Options = {
strategy: (parameter: FooParameter) => {},
parameter: { foo: 'foo' }
}
const barOptions: Options = {
strategy: (parameter: BarParameter) => {},
parameter: { bar: 'bar' }
}
It should be possible for TypeScript to infer the type of fooOptions.parameter
from fooOptions.strategy
and likewise for barOptions.parameter
from barOptions.strategy
.
Is this currently achievable with TypeScript?