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

InjectionToken<T> is poorly typed #55083

Closed
waterplea opened this issue Mar 28, 2024 · 2 comments
Closed

InjectionToken<T> is poorly typed #55083

waterplea opened this issue Mar 28, 2024 · 2 comments

Comments

@waterplea
Copy link
Contributor

waterplea commented Mar 28, 2024

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Issue

I have made a function that expects a particular value from DI, say:

function getNumber(token: ProviderToken<number>): number {
  return inject(token);
}

There is no type error if I try to call it with following token:

const token = new InjectionToken<string>('');

Proposition

This happens because <T> in InjectionToken is only used inside factory function that you can pass inside constructor. I suggest we add a dummy prop inside InjectionToken class to protect against this issue:

/** @internal */
private readonly _guard!: T;

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-crs76u

Please provide the exception or error you saw

No type error when using `InjectionToken` of a wrong type

Please provide the environment you discovered this bug in (run ng version)

17.2.0

Anything else?

No response

@JoostK
Copy link
Member

JoostK commented Mar 28, 2024

Closing as a duplicate of #46815

@JoostK JoostK closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2024
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants