angular: AoT error with `@Inject` and interface type
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/compiler-cli@2.1.2
@angular/core@2.1.2
- Node (for AoT issues):
node --version
=
Node 5.10.1
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 21
- Comments: 40 (16 by maintainers)
Links to this issue
Commits related to this issue
- fix(compiler): support interface types in injectable constuctors Fixes #12631 — committed to chuckjaz/angular by chuckjaz 7 years ago
- fix(compiler): support interface types in injectable constuctors Fixes #12631 — committed to chuckjaz/angular by chuckjaz 7 years ago
- fix(compiler): support interface types in injectable constuctors Fixes #12631 — committed to chuckjaz/angular by chuckjaz 7 years ago
- fix(compiler): support interface types in injectable constuctors Fixes #12631 — committed to chuckjaz/angular by chuckjaz 7 years ago
- fix(compiler): support interface types in injectable constuctors (#14894) Fixes #12631 — committed to angular/angular by chuckjaz 7 years ago
- fix(compiler): support interface types in injectable constuctors (#14894) Fixes #12631 — committed to SamVerschueren/angular by chuckjaz 7 years ago
- fix(ripple): explicit type for global ripple options Due to a bug (https://github.com/angular/angular/issues/12631#event-1001209999), specifiying a type for a injected provider didn't work in AOT mod... — committed to devversion/material2 by devversion 7 years ago
- fix(ripple): explicit type for global ripple options (#4240) Due to a bug (https://github.com/angular/angular/issues/12631#event-1001209999), specifiying a type for a injected provider didn't work in... — committed to angular/components by devversion 7 years ago
- fix(compiler): support interface types in injectable constuctors (#14894) Fixes #12631 — committed to asnowwolf/angular by chuckjaz 7 years ago
- fix(compiler): support interface types in injectable constuctors (#14894) Fixes #12631 — committed to juleskremer/angular by chuckjaz 7 years ago
@MadUser looks like it doesn’t work with ts interfaces such as
Window
,Document
etc. See https://github.com/angular/angular/issues/15640@skdhir for example:
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
any
type in the constructor) was to create a dummy class type to trick the compiler, like:Still happening on v2.4.4. Trying to inject angular’s
DOCUMENT
token:https://github.com/angular/angular/pull/14894 the fix
@dscheerens see my version of this in this https://github.com/angular/angular/issues/12631#issuecomment-274260009. In TypeScript, a
class
can say itextends
aninterface
rather thanimplements
it, which does not require theclass
to repeat theinterface
members.