Skip to content

Commit 58a79b6

Browse files
crisbetodylhunn
authored andcommitted
fix(migrations): preserve type when using inject decorator (angular#57389)
Updates the migration so that it passes the type as a generic in the case of `@Inject(SOME_TOKEN) foo: SomeType`. This is done for two reasons: 1. It's a fairly common pattern and it ensures that the code can still be compiled. 2. It avoids leaving behind unused imports. PR Close angular#57389
1 parent 4ae66f2 commit 58a79b6

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ function migrateInjectDecorator(
526526
injectedType = arrowFn.body.getText();
527527
}
528528
}
529+
} else if (
530+
// Pass the type for cases like `@Inject(FOO_TOKEN) foo: Foo`, because:
531+
// 1. It guarantees that the type stays the same as before.
532+
// 2. Avoids leaving unused imports behind.
533+
// We only do this for type references since the `@Inject` pattern above is fairly common and
534+
// apps don't necessarily type their injection tokens correctly, whereas doing it for literal
535+
// types will add a lot of noise to the generated code.
536+
type &&
537+
(ts.isTypeReferenceNode(type) ||
538+
(ts.isUnionTypeNode(type) && type.types.some(ts.isTypeReferenceNode)))
539+
) {
540+
typeArguments = [type];
529541
}
530542

531543
return {injectedType, typeArguments};

packages/core/schematics/test/inject_migration_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('inject migration', () => {
114114
``,
115115
`@Directive()`,
116116
`class MyDir {`,
117-
` private foo = inject(FOO_TOKEN);`,
117+
` private foo = inject<Foo>(FOO_TOKEN);`,
118118
`}`,
119119
]);
120120
});
@@ -271,7 +271,7 @@ describe('inject migration', () => {
271271
` ]`,
272272
`})`,
273273
`class MyDir {`,
274-
` private foo = inject(FOO_TOKEN, { optional: true });`,
274+
` private foo = inject<Foo>(FOO_TOKEN, { optional: true });`,
275275
`}`,
276276
]);
277277
});
@@ -1431,7 +1431,7 @@ describe('inject migration', () => {
14311431
`@Directive()`,
14321432
`class MyDir {`,
14331433
` private foo = inject(Foo);`,
1434-
` readonly bar = inject(BAR_TOKEN);`,
1434+
` readonly bar = inject<Bar>(BAR_TOKEN);`,
14351435
``,
14361436
` private value: number = this.foo.getValue();`,
14371437
` private otherValue: string = this.bar.getOtherValue();`,
@@ -1470,7 +1470,7 @@ describe('inject migration', () => {
14701470
`@Directive()`,
14711471
`class MyDir {`,
14721472
` private foo = inject(Foo);`,
1473-
` readonly bar = inject(BAR_TOKEN);`,
1473+
` readonly bar = inject<Bar>(BAR_TOKEN);`,
14741474
``,
14751475
` private value: number = this.foo.getValue();`,
14761476
` private otherValue: string;`,
@@ -1599,7 +1599,7 @@ describe('inject migration', () => {
15991599
`@Directive()`,
16001600
`class MyDir {`,
16011601
` private foo = inject(Foo);`,
1602-
` readonly bar = inject(BAR_TOKEN);`,
1602+
` readonly bar = inject<Bar>(BAR_TOKEN);`,
16031603
``,
16041604
` private 'my-value': number = this.foo.getValue();`,
16051605
` private 'my-other-value': string = this.bar.getOtherValue();`,

0 commit comments

Comments
 (0)