NestJs Type Error – Argument of type 'undefined' is not assignable to parameter of type 'string | symbol'

huangapple go评论67阅读模式
英文:

NestJs Type Error - Argument of type 'undefined' is not assignable to parameter of type 'string | symbol'

问题

Here's the translated content:

突然,在NestJs中使用InjectRepository装饰器时,我遇到了类型错误。这个错误在每个服务中都会发生。

我收到了以下错误消息:
无法解析参数装饰器的签名,当作为表达式调用时。
类型为'undefined'的参数无法分配给类型'string | symbol'。ts(1239)

我真的不知道为什么会出现这个错误。

英文:

Suddenly i get an type error when using the InjectRepository decorator in NestJs. This error happens in every service.

 constructor(
    private userRepository: UserRepository,

    @InjectRepository(Workout) <--- Error
    private workoutRepository: Repository<Workout>,
  ) {}
{
  "compilerOptions": {
    "module": "CommonJS",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "strictBindCallApply": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "resolveJsonModule": true
  }
}

I get the following error:
Unable to resolve signature of parameter decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'string | symbol'.ts(1239)

I really don't know why this appears.

答案1

得分: 4

我遇到了这个链接错误,使用 @Inject(forwardRef(() => Class))

这可以通过将 @nestjs/common 更新到 v9.3.0 来解决。

显然,这是由 TypeScript v5 引入的问题。
"[它] 引入了对装饰器类型检查的一些收紧。"<sup>1</sup>

Inject 的工作原理:

export declare const Inject:
  (entity: Function) =&gt;
    (target: object, key: string | symbol, index?: number) =&gt; void;

export class Foo {}

export class C {
  constructor(@Inject(Foo) x: any) {}
}

正如你所看到的,Inject 需要第二个参数,要么是一个 symbol,要么是一个 string

NestJS v9.3.0 已经在这个合并的 PR 下解决了这个问题:Constructor parameter decorators should allow undefined as the type of key #10959

英文:

I've had this lint error raised using @Inject(forwardRef(() =&gt; Class)).

It can be solved by updating @nestjs/common to v9.3.0.

Apparently, this was an issue introduced by TypeScript v5.
"[it] introduces some tightening on decorator type-checking."<sup>1</sup>

How Inject worked:

export declare const Inject:
  (entity: Function) =&gt;
    (target: object, key: string | symbol, index?: number) =&gt; void;

export class Foo {}

export class C {
  constructor(@Inject(Foo) x: any) {}
}

As you can see, Inject requires a second argument that's either a symbol or a string.

NestJS v9.3.0 has this solved under this merged PR: Constructor parameter decorators should allow undefined as the type of key #10959

huangapple
  • 本文由 发表于 2023年4月13日 16:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定