-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Return type inference broken for types with static members in ts 2.5; later fixed #20102
Comments
@alexeagle you could work around this temporarily by using the following signature for declare function get<T>(token: new (...args: any[]) => T, notFoundValue?: T): T; where in 2.5.3 // tslint:disable
interface Type<T> extends Function {
new(...args: any[]): T;
}
declare function get<T>(token: new (...args: any[]) => T, notFoundValue?: T): T;
class Token0 {
a = 1;
}
class Token1 {
static a = 1;
}
let t0 = get(Token0); // Token0
let t1 = get(Token1); // Token1 |
Thanks for the suggestion @aluanhaddad ! |
@DanielRosenwasser what does "In Discussion" mean here? Is there a concrete suggestion to be considered? |
@RyanCavanaugh the suggestion is backporting whatever changes were applied to 2.5.x, which we don't typically do. The bug is already fixed, which is why I didn't mark it as such. |
@alexeagle you should be able to add an overload that takes the existing types to preserve the current behavior. The new signature will need to be declared first since it is more specific. abstract get<T>(token: new (...args: any[]) => T, notFoundValue?: T): T;
abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T): T; |
Angular's dependency injection looks something like this:
As stated in those comments, starting in TS 2.5 we get the wrong type when a user asks the
Injector
for a Token with a static member. It's later fixed in TS 2.6.Workaround is to give an explicit type argument to
get
, eg Angular users should repeat the type likeInjector.get<Token1>(Token1)
There may be no action required here other than asking users to upgrade to TS 2.6 - but sadly Angular doesn't support it yet because we need to upgrade all the places we depend on first. Filing this issue mostly so we have a place to point to in the Angular changelog.
The text was updated successfully, but these errors were encountered: