使用WINSTON_MODULE_PROVIDER注入Logger遇TypeScript ts(1239)报错
背景
维护该项目近一年,一直通过WINSTON_MODULE_PROVIDER令牌注入Winston的Logger,从未出现问题。上周发现TypeScript将@Inject(WINSTON_MODULE_PROVIDER)代码标红,报错信息如下:
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)(alias) const WINSTON_MODULE_PROVIDER: "winston"
import WINSTON_MODULE_PROVIDERNo quick fixes available
原本以为是VSCode问题,关闭项目后今日重新打开,问题依然存在,期间仅更新过VSCode。不过项目仍能正常运行。
代码片段
import { Inject, Injectable } from '@nestjs/common'; import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; import { Logger } from 'winston'; //... @Injectable() export class QueryTransactionsService { constructor( @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, //... ) {} //... }
环境信息
相关依赖版本
- vscode: 1.77.0
- node: 16.13.0
- @nestjs/common: ^9.0.0
- typescript: ^4.3.5
- @types/node: ^16.0.0
- ts-node: ^10.0.0
- winston: ^3.8.2
- nest-winston: ^1.7.0
TypeScript配置
{ "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 } }
已尝试的解决方法
以下方法均未能解决问题:
- 重启VSCode
- 使用
npm i重新安装所有依赖包 - 执行
npm audit fix修复依赖 - 在VSCode设置中将
TypeScript: Enable Prompt Use Workspace Tsdk设为true(参考Stack Overflow相关问题)
内容的提问来源于stack exchange,提问作者Tobi Akanji
相关产品推荐
相关产品推荐

