You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for generic types to the compiler #11057
[ ] bug report => search github for a similar issue or PR before submitting
[x ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
The compiler doesn't emit any generic type parameters and compiling that TypeScript code fails with Generic type ... requires 1 type argument(s)
Expected/desired behavior
The compiler should use generic type information
Angular version: 2.0.0-rc.5
Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
Language: [ TypeScript X.X ]
In my code base, there are a couple of places where I use generic types to help me share code while not lose type information. Up until now, these only existed at compile time and the runtime compiler only saw the JavaScript code without any type information. With the AoT compiler, I currently have to remove all that type information and reduce it to any (shudder), because the compiler just ignores generic parameters. There are two distinct examples in my code base, which I'll put into separate comments.
There is a generic service that provides user settings and within one application (read: bootstrap AppModule), these user settings have exactly one type, but from one application to the next, this type is different. SettingsService<T> is that generic class and my components declare SettingsService<AppSettings> for their constructor argument. Since the generated AppModuleInjector.getInternal only returns any, it seems like it could be sufficient to replace the generic type with any.
This also works with generic constraints. If you decide to be more strict about types in the generated code (like not "routing" the calls through interfaces that return any), this might become more difficult. However, I'm able to specify for the service the generic type parameter at the AppModule level.
Reacted by hydralix, Yosuke Kurami, popinguy and Aluan Haddad
My second use case is a bit more tricky, mostly because deducing the type parameter's type is a real challange.
I have a combination of a component and directive that make up a drop-down control. One is the root element that holds the current value DropDownSelectComponent<T>, the other is instantiated for each choice DropDownSelectOptionDirective<T>. Reduced, it looks like this:
This setup nicely declares the intent of how the component/directive are meant to work together. With runtime compilation, the generic types would be gone and the compiler would easily generate correct code. The AoT compiler currently doesn't emit any generic type parameters and produces code that doesn't compile.
If I could wish for something, I would like to declare that both value members' types define what T should be for that instance. The generated code would then check that the component's T matches that of the ContentChildren. A quick error to make in the example would be
It is by design that we don't support type parameters in components and services.
There are some technical challenges that must be overcome and some TypeScript features that need to be added for this to be implemented correctly that we currently don't believe adding this support provides enough value to justify the cost of its implementation.
Reacted by popinguy, Seth Davenport, Connor Peet, Rushi Soni, Aluan Haddad, Alexander Turtsevich, Sun, Duncan Munro, Salvador Subarroca, Esteban Felix and 15 more
Is there already a list of patterns that the AOT compiler won't support, but work with the JIT compiler?
Is there a way to get an error for unsupported patterns before trying the AOT compiler for the first time?
@Tragetaschen I am working on a document that explains what is supported and what is not but it is taking longer than I expected (my 2 week vacation didn't help much with this).
Right now the only way to get errors that will be reported by the AoT compiler is to run it.
We need at least option to compile the code i.e. just replace all type parameters with any. Example is pretty simple, you could have generic table or generic list and automatically get properly typed selectedItem, dataProvider etc. Actually this example is already implemented and works only in JIT mode.
Sadly not really: In my first example, the class with the generic parameter varies in that parameter depending on the actual application. So there is no way to choose a default parameter. In my second example, the type of the drop down varies per instance and also cannot have a default parameter.
The only way this probably helps is by using any as the default.
On May 23, 2017, at 3:12 PM, Kai Ruhnau ***@***.***> wrote:
Sadly not really: In my first example, the class with the generic parameter varies in that parameter depending on the actual application. So there is no way to choose a default parameter. In my second example, the type of the drop down varies per instance and also cannot have a default parameter.
The only way this probably helps is by using any as the default.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
Activity
Tragetaschen commentedon Aug 25, 2016
There is a generic service that provides user settings and within one application (read: bootstrap
AppModule), these user settings have exactly one type, but from one application to the next, this type is different.SettingsService<T>is that generic class and my components declareSettingsService<AppSettings>for their constructor argument. Since the generatedAppModuleInjector.getInternalonly returnsany, it seems like it could be sufficient to replace the generic type withany.becomes
This also works with generic constraints. If you decide to be more strict about types in the generated code (like not "routing" the calls through interfaces that return
any), this might become more difficult. However, I'm able to specify for the service the generic type parameter at theAppModulelevel.Tragetaschen commentedon Aug 25, 2016
My second use case is a bit more tricky, mostly because deducing the type parameter's type is a real challange.
I have a combination of a component and directive that make up a drop-down control. One is the root element that holds the current value
DropDownSelectComponent<T>, the other is instantiated for each choiceDropDownSelectOptionDirective<T>. Reduced, it looks like this:This setup nicely declares the intent of how the component/directive are meant to work together. With runtime compilation, the generic types would be gone and the compiler would easily generate correct code. The AoT compiler currently doesn't emit any generic type parameters and produces code that doesn't compile.
If I could wish for something, I would like to declare that both
valuemembers' types define whatTshould be for that instance. The generated code would then check that the component'sTmatches that of theContentChildren. A quick error to make in the example would beand suddenly the type of the options is
string, which is not compatible with thebooleandeduced fromSendEmail.hydralix commentedon Sep 14, 2016
Reproduced with angular:2.0.0-rc.7
Please fix it
chuckjaz commentedon Sep 19, 2016
It is by design that we don't support type parameters in components and services.
There are some technical challenges that must be overcome and some TypeScript features that need to be added for this to be implemented correctly that we currently don't believe adding this support provides enough value to justify the cost of its implementation.
Tragetaschen commentedon Sep 20, 2016
Is there already a list of patterns that the AOT compiler won't support, but work with the JIT compiler?
Is there a way to get an error for unsupported patterns before trying the AOT compiler for the first time?
chuckjaz commentedon Sep 20, 2016
@Tragetaschen I am working on a document that explains what is supported and what is not but it is taking longer than I expected (my 2 week vacation didn't help much with this).
Right now the only way to get errors that will be reported by the AoT compiler is to run it.
kemsky commentedon Jan 9, 2017
We need at least option to compile the code i.e. just replace all type parameters with
any. Example is pretty simple, you could have generic table or generic list and automatically get properly typedselectedItem,dataProvideretc. Actually this example is already implemented and works only in JIT mode.chuckjaz commentedon Jan 9, 2017
@kemsky That is currently apart of the work for #12398. This is scheduled for 4.0 and should be available in beta form before the end of the month.
shlomiassaf commentedon May 23, 2017
@Tragetaschen , @chuckjaz
FYI:
This is not an issue now with Generic parameter defaults introduces in TS 2.3
Tragetaschen commentedon May 23, 2017
Sadly not really: In my first example, the class with the generic parameter varies in that parameter depending on the actual application. So there is no way to choose a default parameter. In my second example, the type of the drop down varies per instance and also cannot have a default parameter.
The only way this probably helps is by using
anyas the default.shlomiassaf commentedon May 23, 2017
whyvrafvr commentedon Jun 7, 2017
Well, let's move my abstract generic components into concrete components with any LOL Cheers
angular-automatic-lock-bot commentedon Sep 11, 2019
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.