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 published provideClass return type inference. #14

Merged
merged 3 commits into from
Nov 28, 2024

Conversation

kburov-sc
Copy link
Collaborator

Without the type assertion, compiler resulting npm package type definition for provideClass looks like:

providesClass: <Token extends TokenType, Service, Tokens extends readonly ValidTokens<Services>[]>(token: Token, cls: InjectableClass<Services, Service, Tokens>) => Container<AddService<Services, Token, import("./Injectable").ConstructorReturnType<InjectableClass<Services, Service, Tokens>>>>;

The last bit (ConstructorReturnType<InjectableClass<Services, Service, Tokens>>>>) is somehow problematic. Resolving types for services provided using the function breaks, e.g. container.providesClass('serivice', Service) returns a container of type Container<{ service: any }>.

The fix addresses the issue by asserting the function's return type, so the generated function signature looks like:

providesClass: <Token extends TokenType, Service, Tokens extends readonly ValidTokens<Services>[]>(token: Token, cls: InjectableClass<Services, Service, Tokens>) => Container<AddService<Services, Token, Service>>;

With that container.providesClass('serivice', Service) correctly returns a container of type Container<{ service: Service }>.

src/Container.ts Outdated
@@ -421,7 +421,7 @@ export class Container<Services = {}> {
providesClass = <Token extends TokenType, Service, Tokens extends readonly ValidTokens<Services>[]>(
token: Token,
cls: InjectableClass<Services, Service, Tokens>
) => this.providesService(ClassInjectable(token, cls));
) => this.providesService(ClassInjectable(token, cls)) as Container<AddService<Services, Token, Service>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we have return type explicitly declared on this method. There is a eslint rule for this, I'm thinking to enable it. That will not solve our problem, but that will prevent similar issues to happen in future when TS infers type incorrectly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! 93365db

@kburov-sc kburov-sc merged commit 5b2c728 into main Nov 28, 2024
1 check passed
@kburov-sc kburov-sc deleted the kb-fix-constructor-return-type branch November 28, 2024 06:39
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.

2 participants