Skip to content

Rule Request: [prefer-inject] Prefer injecting dependencies via inject instead of constructor arguments #2165

Closed
@NateRadebaugh

Description

@NateRadebaugh

[prefer-inject] Prefer injecting dependencies via inject instead of constructor arguments

https://angular.dev/guide/di/dependency-injection#injecting-consuming-a-dependency

The most common way to inject a dependency is to declare it in a class constructor. When Angular creates a new instance of a component, directive, or pipe class, it determines which services or other dependencies that class needs by looking at the constructor parameter types. For example, if the HeroListComponent needs the HeroService, the constructor can look like this:

@Component({})
class HeroListComponent {
  constructor(private service: HeroService) {}
}

Another option is to use the inject method:

@Component({})
class HeroListComponent {
  private service = inject(HeroService);
}

I'd love to be able to prefer the inject(HeroService) format, with auto-fixer

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: eslint-pluginAngular-specific TypeScript rulestriageThis issue needs to be looked at and categorized by a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions