graphql-hive / graphql-hive/graphql-modules

Utility type to extract InjectionToken target type and avoid repetition

Open
#2,134 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.3k
Forks
113
Avg merge
3h 36m
Merged PRs (30d)
1

Description

**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.

```ts
const MyToken = new InjectionToken>>('my-token');

class MyClass {
constructor(@Inject(MyToken) someField: Promise>) {}
}
```

**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:
```ts
export type InjectionTokenTargetType = T extends InjectionToken
? U
: never;
```
one can rewrite the previous example like so:
```ts
type MyTokenTargetType = InjectionTokenTargetType;

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**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.