I'm trying to infer the return type of the function via conditional types.
If the type and parameters extend a type, AudioConfig<T> for example - I want the return type to be Audio<T>, otherwise Video<T> and lastly never if type not found.
interface Audio<T extends string> {}
interface Video<T extends string> {}
type AudioConfig<T extends string> = {
type: 'Audio'
}
type VideoConfig<T extends string> = {
type: 'Video'
}
type Params<T extends string> =
| AudioConfig<T>
| VideoConfig<T>
type Configuration<T extends string, P extends Params<T>> =
P extends AudioConfig<T> ? Audio<T> :
P extends VideoConfig<T> ? Video<T> :
never
type InitConfigurationFn = <T extends string, P extends Params<T>>(params: P) => Configuration<T, P>
const initConfiguration: InitConfigurationFn = (params) => {
switch (params.type) {
case 'Audio':
return { type: 'Audio' }
case 'Video':
return { type: 'Video' }
}
}
Currently initConfiguration function is erroring:
Type '<T extends string, P extends Params>(params: P) => { type: string; }' is not assignable to type 'InitConfigurationFn'. Type '{ type: string; }' is not assignable to type 'Configuration<T, P>'.(2322)