darraghoriordan / eslint-plugin-nestjs-typed

Some eslint rules for working with NestJs projects
http://www.darraghoriordan.com
171 stars 34 forks source link

add rule to disalow using `@Inject()` without some token when using it on `constructor` #116

Closed micalevisk closed 3 months ago

micalevisk commented 11 months ago

The usage of @Inject() without some provider's token on parameters is invalid (due to TS limitations, AFIAK). And I didn't succeded on making it fail at build time. So, as of now, we will see a 'dependency not found' error at runtime. This could confuse people.

My suggestion is adding a rule to forbids such usage

image

code ```ts import { Module, Inject, forwardRef } from '@nestjs/common'; class FooService {} @Module({ providers: [FooService] }) export class AppModule { @Inject(forwardRef(() => FooService)) public readonly service: FooService; @Inject() private readonly validInjectUsage: FooService constructor( /* @Inject(forwardRef(() => FooService)) private valid: FooService , @Inject(undefined) private valid2: FooService , @Inject('') private valid3: FooService // , */ @Inject() private invalid: FooService // <<< this should error out as 'you must pass a token' ) {} } ```
darraghoriordan commented 11 months ago

oh interesting. nice suggestion. i'll try to do this sometime this year if i get time 👍

just thinking (i havent tested) - but is this maybe an esmodule import / export issue ? I know es modules a bit weird about declaring multiple classes in one file and circular references sometimes

micalevisk commented 11 months ago

but the final code (after tsc compilation) will be in CJS, not in ESM

and I tried with classes declared in separated files as well. I don't think this is a circular related issue

micalevisk commented 3 months ago

This is supported on Nestjs now