-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Description
🐞 bug report
Affected Package
@angular/core, ES2015 application; ReflectionCapabilities
Is this a regression?
Nope, I think it's an issue that has been there for a long time.
Description
Consider you have the following TypeScript file. Component A extends another component B, but
does have a public property defined in A.
Problem is that TypeScript in ES2015 mode creates a pass-through constructor just in order to initialize the public property that causes the compiler to not inherit metadata from B.
Here is the code (below some more details about the issue)
@Component({
...
})
export class CdkBaseComponent {
constructor (myService: SomeService) { ... }
}
@Component({
...
})
export class EnhancedComponent extends CdkBaseComponent {
somePublicProperty = "my_default_value"
}
This TypeScript code turns into:
// ...
export class EnhancedComponent extends CdkBaseComponent {
constructor() {
super(...arguments);
this.somePublicProperty = "my_default_value";
}
}
EnhancedComponent.decorators = [
{ type: Component, args: [{
template: 'Enhanced Component Template',
},] },
];
As you can see, TypeScript generated a pass-through constructor that just passes ...arguments
to the superclass. This means that @angular/core
detects a constructor for the class and doesn't bother checking the superclass metadata.
Maybe there should be a ES2015 variant for delegate constructors?
export const DELEGATE_CTOR = /^function\s+\S+\(\)\s*{[\s\S]+\.apply\(this,\s*arguments\)/; |
🔬 Minimal Reproduction
Code above; or some easy to check-out TypeScript file with tsconfig
.
https://gist.github.com/DevVersion/c84f196d68a549e3cbbaacdfccd3aab5
Run ngc -p tsconfig-test.json
and inspect output.
🌍 Your Environment
Angular Version:
7.1.0
Anything else relevant?
This is an issue we run into with Angular Material and the CDK.
Potential abandoned fix or related issues:
- Refactor ReflectionCapabilities to rely on metadata for identifying 'pass-through' constructors #22642
- Inheriting constructor with Jit mode fails when using closure compiler or property initializers #13211
A workaround would be to explicitly create a constructor with parameters, but in the long-term, this makes the source-code unnecessarily large.
cc. @jelbourn