Skip to content

Commit 27761b4

Browse files
committed
fix(compiler): do not report type errors for arguments with @Inject (#16222)
Fixes #15424
1 parent a80ac0a commit 27761b4

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

packages/compiler/src/aot/static_reflector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,16 @@ export class StaticReflector implements CompileReflector {
182182
const ctorData = members ? members['__ctor__'] : null;
183183
if (ctorData) {
184184
const ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
185-
const parameterTypes = <any[]>this.simplify(type, ctor['parameters'] || []);
185+
const rawParameterTypes = <any[]>ctor['parameters'] || [];
186186
const parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators'] || []);
187187
parameters = [];
188-
parameterTypes.forEach((paramType, index) => {
188+
rawParameterTypes.forEach((rawParamType, index) => {
189189
const nestedResult: any[] = [];
190-
if (paramType) {
191-
nestedResult.push(paramType);
192-
}
190+
const paramType = this.trySimplify(type, rawParamType);
191+
if (paramType) nestedResult.push(paramType);
193192
const decorators = parameterDecorators ? parameterDecorators[index] : null;
194193
if (decorators) {
194+
if (paramType) nestedResult.push(paramType);
195195
nestedResult.push(...decorators);
196196
}
197197
parameters !.push(nestedResult);

packages/compiler/test/aot/static_reflector_spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,48 @@ describe('StaticReflector', () => {
570570
expect(annotation.providers).toEqual([]);
571571
});
572572

573+
// #15424
574+
it('should be able to inject a ctor parameter with a @Inject and a type expression', () => {
575+
const data = Object.create(DEFAULT_TEST_DATA);
576+
const file = '/tmp/src/invalid-component.ts';
577+
data[file] = `
578+
import {Injectable, Inject} from '@angular/core';
579+
580+
@Injectable()
581+
export class SomeClass {
582+
constructor (@Inject('some-token') a: {a: string, b: string}) {}
583+
}
584+
`;
585+
init(data);
586+
587+
const someClass = reflector.getStaticSymbol(file, 'SomeClass');
588+
const parameters = reflector.parameters(someClass);
589+
expect(parameters.toString()).toEqual('@Inject');
590+
});
591+
592+
it('should reject a ctor parameter without a @Inject and a type exprssion', () => {
593+
const data = Object.create(DEFAULT_TEST_DATA);
594+
const file = '/tmp/src/invalid-component.ts';
595+
data[file] = `
596+
import {Injectable} from '@angular/core';
597+
598+
@Injectable()
599+
export class SomeClass {
600+
constructor (a: {a: string, b: string}) {}
601+
}
602+
`;
603+
604+
let error: any = undefined;
605+
init(data, [], (err: any, filePath: string) => {
606+
expect(error).toBeUndefined();
607+
error = err;
608+
});
609+
610+
const someClass = reflector.getStaticSymbol(file, 'SomeClass');
611+
expect(reflector.parameters(someClass)).toEqual([[]]);
612+
expect(error).toBeUndefined();
613+
});
614+
573615
describe('inheritance', () => {
574616
class ClassDecorator {
575617
constructor(public value: any) {}

0 commit comments

Comments
 (0)