You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 08:18:16