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

🏷️ fix(strongly-typed) allow use of generic helpers #173

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

alecgibson
Copy link
Contributor

Fixes #170

At the moment it's impossible to create generic helper functions to deal with TypedContainer:

interface MyMap {
  foo: string;
}

function fn<T extends MyMap>(container: TypedContainer<T>) {
  container.get('foo') // error
}

This is because of the Synchronous type that guards against calling .get() on Promise bindings. Under the hood, this type is a mapped type, which doesn't work well with generics (by design).

Rather than drop this guard all together, this change aims to strike a balance by removing the Synchronous mapped type, and instead changing the return type of synchronous get() methods to be never if the binding is a Promise.

This won't error as obviously or as immediately as before, but will still at least flag to the developer semantically that this binding will never return a value (since it will throw), and should cause compilation errors if consumers try to do anything with the returned value.

In return, we gain the ability to use generic helper functions.

Fixes inversify#170

At the moment it's impossible to create generic helper functions to
deal with `TypedContainer`:

```ts
interface MyMap {
  foo: string;
}

function fn<T extends MyMap>(container: TypedContainer<T>) {
  container.get('foo') // error
}
```

This is because of the `Synchronous` type that guards against calling
`.get()` on `Promise` bindings. Under the hood, this type is a mapped
type, which [doesn't work well with generics][1] (by design).

Rather than drop this guard all together, this change aims to strike a
balance by removing the `Synchronous` mapped type, and instead changing
the return type of synchronous `get()` methods to be `never` if the
binding is a `Promise`.

This won't error as obviously or as immediately as before, but will
still at least flag to the developer semantically that this binding will
never return a value (since it will throw), and should cause compilation
errors if consumers try to do anything with the returned value.

In return, we gain the ability to use generic helper functions.

[1]: microsoft/TypeScript#35647
@notaphplover notaphplover merged commit c5b382b into inversify:main Dec 10, 2024
3 checks passed
@alecgibson alecgibson deleted the fix-strong-generic branch December 10, 2024 11:14
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 this pull request may close these issues.

[Bug]: Can't use generic helpers with TypedContainer in strongly-typed
2 participants