ngc with `strictMetadataEmit: true` error on `@inject()` with generic type.
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:
Current behavior
When you inject a token with @Inject()
and generic type, compiling with strictMetadataEmit: true
causes the following error:
Metadata collected contains an error that will be reported at runtime: Could not resolve type XXX
I’m using Partial<T>
so at first I thought this was related to typescript built-in types but I created myself an export type Optional<T>
and still got the same error.
Expected behavior
No error.
Minimal reproduction of the problem with instructions
export interface Setting {
a: string;
}
export const SETTING = new InjectionToken<Partial<Setting>>('');
export class Foo {
constructor(@Inject(SETTING) setting: Partial<Setting>){ }
}
What is the motivation / use case for changing the behavior?
I’m building a lib where user can inject some settings. They’re all optional as missing ones will be filled with default values.
Environment
Angular version: 5.2.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Angular compiler options
When true (recommended), reports an error for a supplied parameter whose injection type cannot be determined. When false (currently the default), constructor ...
Read more >Injection of Generic Services in Angular - Stack Overflow
This is a simple solution: // service and models export class User { firstName: string } export class Admin { lastName: string } ......
Read more >Generic CRUD Service & Models in Angular | by Nikos Anifantis
We use T generic type and we extend it to be string | number . Thus, if we try to write something like...
Read more >AngularJS to Angular Quick Reference
If you want `ngc` to report syntax errors immediately rather than produce a `.metadata.json` file with errors, set the `strictMetadataEmit` option in ...
Read more >angular cannot create an instance of an abstract class
//created an abstract class (constructor function) function employee() { this.empname= "empname"; if(this.constructor === employee){ throw new error("fyi: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just got hit by this. ):
Will
@Inject(DOCUMENT) documentRef: Document
ever be supported without@dynamic
? This seems to be the one opened issue related to this case.