question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ngc with `strictMetadataEmit: true` error on `@inject()` with generic type.

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
alfaprojectcommented, May 13, 2018

Just got hit by this. ):

2reactions
waterpleacommented, Feb 27, 2019

Will @Inject(DOCUMENT) documentRef: Document ever be supported without @dynamic? This seems to be the one opened issue related to this case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found