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

Utility type to extract InjectionToken target type and avoid repetition #2134

Open
gthau opened this issue Mar 9, 2022 · 1 comment
Open

Comments

@gthau
Copy link

gthau commented Mar 9, 2022

Is your feature request related to a problem? Please describe.

When doing dependency injection in the constructor using an injection token, one has to know the target type of the injection token and repeat this type in the constructor, e.g.

const MyToken = new InjectionToken<Promise<Array<string | number>>>('my-token');

class MyClass {
  constructor(@Inject(MyToken) someField: Promise<Array<string | number>>) {}
}

Describe the solution you'd like

I'd like graphql-module to provide me with a utility type to extract the target type of the injection token.
Using this simple utility type:

export type InjectionTokenTargetType<T> = T extends InjectionToken<infer U>
  ? U
  : never;

one can rewrite the previous example like so:

type MyTokenTargetType = InjectionTokenTargetType<typeof MyToken>;

class MyClass {
  constructor(@Inject(MyToken) someField: MyTokenTargetType) {}
}

Describe alternatives you've considered

I have written this utility type in my project code. However I believe it'd beneficial to have it in graphql-modules. Such a type is not currently writeable as a generic Typescript type because Typescript doesn't natively support Higher-Kinded types (parametrized generic types).

Additional context

@gthau
Copy link
Author

gthau commented Mar 9, 2022

Related PR: #2135

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

No branches or pull requests

1 participant