Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation failure with Generic, Promis and ReturnType #40762

Closed
ilchenkoArtem opened this issue Sep 25, 2020 · 2 comments
Closed

Compilation failure with Generic, Promis and ReturnType #40762

ilchenkoArtem opened this issue Sep 25, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ilchenkoArtem
Copy link

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]>'.

Playground Link:
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJICECuBnUFvYCqIwYAsgPYAmEANsgN4BQyyADnFBOKlQPwAuZCEwBbAEbQANC2TcAbsCgUQo7mF6DhYyVCYBfJk1CRYiFKgCiIRctXrKNes1YBzTMH5DsYKKFcyrFxwVCq0AJ7IoAAW0KRwIEhaPn4gAbI40AAiFKJwoJpCIhLSsjB+3FQRAHJwasm+-gZGJtDwSGhZwAhgwCqcwPiMsgpKKmrgQlY2Y-bgjnQA2gC6ANyy4jh4BMSkU1i4IPhEJOTUS2vNTGDhbChdPX0gnOFkEGDR1NgAKrcoALzDViLADWEEioGQYPCFBgnW6vX6FWwyyEAAoAJTIf4APmQAAVlKJgNgIAAeVAPRHPZGg8HLHEGdZMODYcKJZAwTCJanIWgUEJUp4vMnfHFozBQWgCFL+DFCQm5Enk74rPEuZAIFQ+ZBcbBsbUA5BwADu+TAnPeCGiaIABgB6OBsYD2gAkDEltH0tox6yC70lIGNZtIuvwBpApIAdAArbAqTFXLWRi38wUI4VQV7vT5UbACIVI7MfL6-O7YoFyGZ2CZgdFY3F8gVUQs08IU6y2cYOc60cUAclGNfU2H7GMCyE2h2OuzryEx2LxaZbGaLFIO2xOpAWfbR-anm9nY5khiYyZ1y9bL3Q4VqamxsjJ1TkAA9ICA81DwbDkFes28SzzMsIHFZ41CEap5UVYlSTJAAlAMoBAYCyT-Ytcx+P5FmqBk8UbS9VzbACMMWMCIGWRMgA

Related Issues:

@ilchenkoArtem ilchenkoArtem changed the title Compilation failure with Generic Primis and ReturnType Compilation failure with Generic, Promis and ReturnType Sep 25, 2020
@MartinJohns
Copy link
Contributor

Simplified example code:

type Methods = { a: () => 'a', b: () => 'b' }
declare const methods: Methods;
const load = <K extends keyof Methods>(key: K): ReturnType<Methods[K]> => methods[key]();

Looks like a duplicate of #31811 to me.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 25, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants