Repository navigation
Closed
Description
I'm submitting a ... (check one with "x")
[x] bug report => search github for a similar issue or PR before submitting
[ ] 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
I have a constructor that uses @Inject to get a dependency, and references an interface for that the dep, constructor( @Inject(FirebaseApp) _fbApp: firebase.app.App). When I compile the lib with ngc, I get this error:
Error: angularfire2/src/auth/firebase_sdk_auth_backend.ts:27:1: Error encountered in metadata generated for exported symbol 'FirebaseSdkAuthBackend':
angularfire2/src/auth/firebase_sdk_auth_backend.ts:30:45: Metadata collected contains an error that will be reported at runtime: Expression form not supported.
{"__symbolic":"error","message":"Expression form not supported","line":29,"character":44}
at angularfire2/node_modules/@angular/tsc-wrapped/src/collector.js:514:27
at Array.forEach (native)
at validateMetadata (angularfire2/node_modules/@angular/tsc-wrapped/src/collector.js:502:42)
at MetadataCollector.getMetadata (angularfire2/node_modules/@angular/tsc-wrapped/src/collector.js:374:17)
at MetadataWriterHost.writeMetadata (angularfire2/node_modules/@angular/tsc-wrapped/src/compiler_host.js:111:51)
at MetadataWriterHost.writeFile (angularfire2/node_modules/@angular/tsc-wrapped/src/compiler_host.js:103:19)
at Object.writeFile (angularfire2/node_modules/typescript/lib/typescript.js:45364:132)
at Object.writeFile (angularfire2/node_modules/typescript/lib/typescript.js:7597:14)
at writeEmittedFiles (angularfire2/node_modules/typescript/lib/typescript.js:37854:20)
at doEmit (angularfire2/node_modules/typescript/lib/typescript.js:37714:17)
If I change the type to any, compilation succeeds.
Expected behavior
I would expect to be able to reference an interface in a constructor in conjunction with @Inject.
Minimal reproduction of the problem with instructions
ngc this thing:
import { NgModule, Inject, Injectable } from '@angular/core';
import { Http } from '@angular/http';
export interface Foo {}
@Injectable()
export class MyClass {
constructor(@Inject(Http) http: Foo) {}
}
@NgModule({
providers: [MyClass]
})
export class MyModule {}
- Angular version: 2.0.X
@angular/[email protected]
@angular/[email protected]
- Node (for AoT issues):
node --version=
Node 5.10.1
Reacted by James Birtles, Psau, Valentin, binarious, James Henry, Alexander Turtsevich, Oliver Janik, Vyacheslav, Ruslan Arkhipau, Yuta Moriyama and 11 more
Metadata
Metadata
Assignees
Labels
No labels
Activity
vicb commentedon Oct 31, 2016
/cc @chuckjaz
filipesilva commentedon Nov 14, 2016
I am also experiencing this with
@types/angular-routerwhile upgrading an app with@angular/upgrade/static:Further details in #12860.
psaussure commentedon Dec 12, 2016
Same here by using
@Inject(FirebaseApp) private firebaseApp: firebase.app.Appin my constructor. I confirm the @jeffbcross error and agree we shouldn't have to type using
anyto get the intended behavior.ValentinFunk commentedon Dec 14, 2016
Getting the same thing
Fails with
mmc41 commentedon Dec 28, 2016
Getting the same thing with a test component (AOT really miss a feature to exclude things):
The component is only used for testing but still AOT insist it should be defined in a module (otherwise another error will occur) and when processing AOT produces this error:
A complete blocker - could not be more serious. Only way forward is to delete all my advanced tests in order to get AOT working... NO
DzmitryShylovich commentedon Dec 28, 2016
but u can just change type to any... or change interface to abstract class
mmc41 commentedon Dec 28, 2016
Thanks. This workaround works too. Annoying that I lose type safety here but at least I can proceed.
DzmitryShylovich commentedon Dec 28, 2016
I believe u can do something like
mmc41 commentedon Dec 28, 2016
Yes indeed. I actually did exactly what you suggest with an additional comment referencing this bug to explain why I had to write bad code like this (it hurts to write 'any', to do type conversions and to add seemingly unnecessary lines of code ;-))
oliverjanik commentedon Jan 11, 2017
Wow this took a long time to find on Google. This is still happening in 2.4.1 using @ngtools/webpack npm package. Why does AOT even care about constructor arguments?
chuckjaz commentedon Jan 11, 2017
In this case it shouldn't. In general, if no
@Inject()is provided, the type of the argument is the type of the instance created by the injector. This bug is tracking that non-reified (such as interfaces) are handled correctly by the collector and the static reflector.itsnotvalid commentedon Jan 18, 2017
Provider Alias also depend of
@Inject()which produces the same error.Meligy commentedon Jan 21, 2017
I am having this as well.
This fails with AoT compiler in Angular CLI, because
Window(the browser window) is an interface:With error:
(More details in #14050)
The workaround (other than using
anytype in the constructor) was to create a dummy class type to trick the compiler, like:24 remaining items
DzmitryShylovich commentedon Mar 11, 2017
#14894 the fix
fix(compiler): support interface types in injectable constuctors
fix(compiler): support interface types in injectable constuctors (#14894
MadUser commentedon Mar 30, 2017
Are you sure this works with ng build --prod?
because for me it still breaks
DzmitryShylovich commentedon Mar 30, 2017
@MadUser it does work using plain ngc. If it doesn't work in cli then it's a cli issue
mmc41 commentedon Mar 30, 2017
Works fine for me with angular cli 1.0 and angular 4.0 project
DzmitryShylovich commentedon Mar 30, 2017
@MadUser looks like it doesn't work with ts interfaces such as
Window,Documentetc. See #15640fix(ripple): explicit type for global ripple options
fix(ripple): explicit type for global ripple options (#4240)
fix(compiler): support interface types in injectable constuctors (ang…
fix(compiler): support interface types in injectable constuctors (ang…
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.