使用class-validator时遭遇属性装饰器签名解析错误求助
问题:class-validator装饰器报错
遇到如下TypeScript错误:
Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'
示例代码:
import { IsEmail } from 'class-validator' class Example { @IsEmail() email: string }
环境说明:
- 已安装class-validator 0.14.0版本
- tsconfig.json仅修改了
"strictPropertyInitialization": false
解决方案
- 确认TypeScript版本兼容性:class-validator 0.14.0要求TypeScript 4.0及以上版本,执行
tsc -v查看当前版本,若版本过低,执行npm install typescript@^4.0.0 --save-dev升级 - 完善tsconfig.json装饰器配置:在compilerOptions中添加或确保开启以下两项:
{ "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true, "strictPropertyInitialization": false } } - 检查文件类型:确保代码文件后缀为
.ts/.tsx,装饰器语法仅支持TypeScript,不能用.js文件 - 重置依赖:若配置无误仍报错,删除
node_modules和package-lock.json,重新执行npm install
内容的提问来源于stack exchange,提问作者Bobface
相关产品推荐
相关产品推荐

