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

[Typescript] Create example of inferring return type depending on argument value #2176

Open
AlexanderArvidsson opened this issue Oct 2, 2020 · 0 comments · May be fixed by #2177
Open

[Typescript] Create example of inferring return type depending on argument value #2176

AlexanderArvidsson opened this issue Oct 2, 2020 · 0 comments · May be fixed by #2177

Comments

@AlexanderArvidsson
Copy link

I recently stumbled across a method of inferring the type of the return value depending on the value of the argument.
The use-case for me was creating an API that would, given a path to a resource, automatically return the correct type for that specific resource.

Here is an example demonstrating intended functionality:

interface SomeType {
  a: number;
}

interface OtherType {
  b: number;
}

// In this case, both a and a2 will be of type SomeType
const a = pathFetch(pathMap.someResource);
const a2 = pathFetch("/v1/some_resource");

// In this case, b will be of type OtherType
const b = pathFetch(pathMap.otherType);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant