Closed
Description
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms:
Promise, ReturnType, Generic
Code
interface IBusinessUnitModel {
parentId?: number,
environmentId?: number
}
interface IEnvironmentModel {
guid?: string,
readonly inheritance?: string,
userDomainId?: number,
friendlyName?: string
}
interface IDictionaries {
environment: IEnvironmentModel[];
businessUnit: IBusinessUnitModel[];
}
type DictionaryMethodsType = {
[key in keyof IDictionaries]: () => Promise<IDictionaries[key]>
};
async function loadDictionary<T>(url:string): Promise<T[]> {
const response = await fetch(`/api/${url}`);
return await response.json()
}
const loadDictionaryMethods:DictionaryMethodsType = {
environment: () => loadDictionary<IEnvironmentModel>('environments'),
businessUnit: () => loadDictionary<IBusinessUnitModel>('businessUnit'),
}
export const loadDictionaryByName =
<N extends keyof DictionaryMethodsType>(name: N):Promise<ReturnType<DictionaryMethodsType[N]>> => loadDictionaryMethods[name]()
Expected behavior:
Compilation successful
Actual behavior:
Compilation failure with error:
Type 'Promise<IEnvironmentModel[]> | Promise<IBusinessUnitModel[]>' is not assignable to type 'Promise<ReturnType<DictionaryMethodsType[N]>>'. Type 'Promise<IEnvironmentModel[]>' is not assignable to type 'Promise<ReturnType<DictionaryMethodsType[N]>>'. Type 'IEnvironmentModel[]' is not assignable to type 'ReturnType<DictionaryMethodsType[N]>'.
Related Issues: