Skip to content

Commit 1cf616f

Browse files
crisbetodylhunn
authored andcommitted
fix(migrations): remove generic arguments from the injected type reference (#57127)
Currently if an injected type has type arguments, we copy it over together with the type arguments to inject, because `inject()` isn't able to infer the generic properly otherwise. E.g. if there's `constructor(el: ElementRef<HTMLElement>)` we produce `inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);`. These changes drop the generics from the `inject()` parameter since we're overwriting the type anyway. The example from above would become `inject<ElementRef<HTMLElement>>(ElementRef);`. PR Close #57127
1 parent 166166d commit 1cf616f

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

packages/core/schematics/ng-generate/inject-migration/migration.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,21 @@ function createInjectReplacementCall(
309309
const sourceFile = param.getSourceFile();
310310
const decorators = getAngularDecorators(localTypeChecker, ts.getDecorators(param) || []);
311311
const literalProps: ts.ObjectLiteralElementLike[] = [];
312-
let injectedType = param.type?.getText() || '';
313-
let typeArguments = param.type && hasGenerics(param.type) ? [param.type] : undefined;
312+
const type = param.type;
313+
let injectedType = '';
314+
let typeArguments = type && hasGenerics(type) ? [type] : undefined;
314315
let hasOptionalDecorator = false;
315316

317+
if (type) {
318+
// Remove the type arguments from generic type references, because
319+
// they'll be specified as type arguments to `inject()`.
320+
if (ts.isTypeReferenceNode(type) && type.typeArguments && type.typeArguments.length > 0) {
321+
injectedType = type.typeName.getText();
322+
} else {
323+
injectedType = type.getText();
324+
}
325+
}
326+
316327
for (const decorator of decorators) {
317328
if (decorator.moduleName !== moduleName) {
318329
continue;
@@ -328,9 +339,7 @@ function createInjectReplacementCall(
328339
// `inject` no longer officially supports string injection so we need
329340
// to cast to any. We maintain the type by passing it as a generic.
330341
if (ts.isStringLiteralLike(firstArg)) {
331-
typeArguments = [
332-
param.type || ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
333-
];
342+
typeArguments = [type || ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)];
334343
injectedType += ' as any';
335344
}
336345
}

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe('inject migration', () => {
166166
``,
167167
`@Directive()`,
168168
`class MyDir {`,
169-
` private one = inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);`,
169+
` private one = inject<ElementRef<HTMLElement>>(ElementRef);`,
170170
` private two = inject<ElementRef<HTMLButtonElement> | ElementRef<HTMLSpanElement>>(ElementRef);`,
171171
`}`,
172172
]);

0 commit comments

Comments
 (0)