|
| 1 | +## Inject migration |
| 2 | +Automated migration that converts classes using constructor-based injection to the `inject` |
| 3 | +function. It can be run using: |
| 4 | + |
| 5 | +```bash |
| 6 | +ng generate @angular/core:inject-migration |
| 7 | +``` |
| 8 | + |
| 9 | +**Before:** |
| 10 | +```typescript |
| 11 | +import { Component, Inject, Optional } from '@angular/core'; |
| 12 | +import { MyService } from './service'; |
| 13 | +import { DI_TOKEN } from './token'; |
| 14 | + |
| 15 | +@Component({}) |
| 16 | +export class MyComp { |
| 17 | + constructor(private service: MyService, @Inject(TOKEN) @Optional() readonly token: string) {} |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +**After:** |
| 22 | +```typescript |
| 23 | +import { Component, Inject, Optional, inject } from '@angular/core'; |
| 24 | +import { MyService } from './service'; |
| 25 | +import { DI_TOKEN } from './token'; |
| 26 | + |
| 27 | +@Component({}) |
| 28 | +export class MyComp { |
| 29 | + private service = inject(MyService); |
| 30 | + readonly token = inject(DI_TOKEN, { optional: true }); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Options |
| 35 | +The migration includes several options to customize its output. |
| 36 | + |
| 37 | +#### `path` |
| 38 | +Determines which sub-path in your project should be migrated. Pass in `.` or leave it blank to |
| 39 | +migrate the entire directory. |
| 40 | + |
| 41 | +#### `migrateAbstractClasses` |
| 42 | +Angular doesn't validate that parameters of abstract classes are injectable. This means that the |
| 43 | +migration can't reliably migrate them to `inject` without risking breakages which is why they're |
| 44 | +disabled by default. Enable this option if you want abstract classes to be migrated, but note |
| 45 | +that you may have to **fix some breakages manually**. |
| 46 | + |
| 47 | +#### `backwardsCompatibleConstructors` |
| 48 | +By default the migration tries to clean up the code as much as it can, which includes deleting |
| 49 | +parameters from the constructor, or even the entire constructor if it doesn't include any code. |
| 50 | +In some cases this can lead to compilation errors when classes with Angular decorators inherit from |
| 51 | +other classes with Angular decorators. If you enable this option, the migration will generate an |
| 52 | +additional constructor signature to keep it backwards compatible, at the expense of more code. |
| 53 | + |
| 54 | +**Before:** |
| 55 | +```typescript |
| 56 | +import { Component } from '@angular/core'; |
| 57 | +import { MyService } from './service'; |
| 58 | + |
| 59 | +@Component({}) |
| 60 | +export class MyComp { |
| 61 | + constructor(private service: MyService) {} |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +**After:** |
| 66 | +```typescript |
| 67 | +import { Component } from '@angular/core'; |
| 68 | +import { MyService } from './service'; |
| 69 | + |
| 70 | +@Component({}) |
| 71 | +export class MyComp { |
| 72 | + private service = inject(MyService); |
| 73 | + |
| 74 | + /** Inserted by Angular inject() migration for backwards compatibility */ |
| 75 | + constructor(...args: unknown[]); |
| 76 | + |
| 77 | + constructor() {} |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +#### `nonNullableOptional` |
| 82 | +If injection fails for a parameter with the `@Optional` decorator, Angular returns `null` which |
| 83 | +means that the real type of any `@Optional` parameter will be `| null`. However, because decorators |
| 84 | +cannot influence their types, there is a lot of existing code whose type is incorrect. The type is |
| 85 | +fixed in `inject()` which can cause new compilation errors to show up. If you enable this option, |
| 86 | +the migration will produce a non-null assertion after the `inject()` call to match the old type, |
| 87 | +at the expense of potentially hiding type errors. |
| 88 | + |
| 89 | +**Note:** non-null assertions won't be added to parameters that are already typed to be nullable, |
| 90 | +because the code that depends on them likely already accounts for their nullability. |
| 91 | + |
| 92 | +**Before:** |
| 93 | +```typescript |
| 94 | +import { Component, Inject, Optional } from '@angular/core'; |
| 95 | +import { TOKEN_ONE, TOKEN_TWO } from './token'; |
| 96 | + |
| 97 | +@Component({}) |
| 98 | +export class MyComp { |
| 99 | + constructor( |
| 100 | + @Inject(TOKEN_ONE) @Optional() private tokenOne: number, |
| 101 | + @Inject(TOKEN_TWO) @Optional() private tokenTwo: string | null) {} |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +**After:** |
| 106 | +```typescript |
| 107 | +import { Component, Inject, Optional, inject } from '@angular/core'; |
| 108 | +import { TOKEN_ONE, TOKEN_TWO } from './token'; |
| 109 | + |
| 110 | +@Component({}) |
| 111 | +export class MyComp { |
| 112 | + // Note the `!` at the end. |
| 113 | + private tokenOne = inject(TOKEN_ONE, { optional: true })!; |
| 114 | + |
| 115 | + // Does not have `!` at the end, because the type was already nullable. |
| 116 | + private tokenTwo = inject(TOKEN_TWO, { optional: true }); |
| 117 | +} |
| 118 | +``` |
0 commit comments