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

feat(core): tighten the typings on Provider.provide #15340

Closed
wants to merge 1 commit into from

Conversation

DzmitryShylovich
Copy link
Contributor

Closes #15339

*/
provide: any;
provide: string|Type<any>|InjectionToken<any>|Function;
Copy link
Contributor Author

@DzmitryShylovich DzmitryShylovich Mar 21, 2017

Choose a reason for hiding this comment

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

After some digging I figured out that Type<any> doesn't match abstract classes (this is why I added Function as well. Didn't find anything better)

export interface Type<T> extends Function { new (...args: any[]): T; }

Abstract class can't be instantiated using new.

So get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T signature doesn't support abstract classes. is this intentional?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you provide an example of how Type<any> does not match? Preferably on http://www.typescriptlang.org/play/

Copy link
Contributor Author

@DzmitryShylovich DzmitryShylovich Mar 22, 2017

Choose a reason for hiding this comment

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

Can you provide an example of how Type does not match?

http://prntscr.com/en7kff
Plus CI failed on abstract classes like PlatformRef when i used

provide: string|Type<any>|InjectionToken<any>;

instead of

provide: string|Type<any>|InjectionToken<any>|Function;

Copy link
Contributor

Choose a reason for hiding this comment

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

I get it now. MyAbstractClass is not matched by Type<any> because it does not have new ().

interface Type<T> {
  new(...args:any[]):T // This is what is missing from abstract classes
}

I wonder if we can expand Type somehow to include abstract classes as well? Why is Type not just extends Function? (@tbosch do you know?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what is missing from abstract classes

yeap, we can't create an abstract class using new.

I wonder if we can expand Type somehow to include abstract classes as well?

I didn't find anything. Here's the same question on StackOverFlow http://stackoverflow.com/questions/36886082/abstract-constructor-type-in-typescript

Looks like we need to remove new(...args:any[]):T and just extend Function

Copy link
Contributor

@mhevery mhevery left a comment

Choose a reason for hiding this comment

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

this has to be split to the old API which still needs to be supported, and new api going forward.

The new API only allows Type and InjectableTokens. Old API supported any (not just string) So this has to be broken in a way so that if you using string than you gate a @deprecated warning where as when you use InjectableToken or Type you do not.

*/
provide: any;
provide: string|Type<any>|InjectionToken<any>|Function;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you provide an example of how Type<any> does not match? Preferably on http://www.typescriptlang.org/play/

*/
provide: any;
provide: string|Type<any>|InjectionToken<any>|Function;
@chuckjaz chuckjaz added area: core Issues related to the framework runtime feature Issue that requests a new feature labels May 23, 2017
@vicb
Copy link
Contributor

vicb commented Feb 15, 2018

closing as outdated

@vicb vicb closed this Feb 15, 2018
@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 Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime cla: yes feature Issue that requests a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tighten the typings on Provider
5 participants