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

Inversify构造函数注入@unmanaged装饰器报TS1239错误求助

解决Inversify构造函数注入中的TS1239错误

错误原因

该错误源于Inversify v6+版本中@unmanaged装饰器的用法变更:新版本的@unmanaged是无参数装饰器,带括号调用会触发TypeScript参数装饰器签名不匹配的报错。

解决方案

1. 修正@unmanaged的写法

直接去掉@unmanaged后的括号,使用无参数形式的装饰器:

import { EntityTarget, Repository } from "typeorm"
import { injectable, unmanaged, inject } from "inversify";

@injectable()
export abstract class RepositoryBase<T extends EntityBase> implements IRepository<T> {
    protected _repository: Repository<T>;
    protected _db: Database;
    constructor(@unmanaged entity: EntityTarget<T>, @inject(Database) db: Database) {
        this._db = db;
        this._repository = this._db.getRepository(entity);
    }
}

2. 补充依赖注入的必要装饰器

给db参数添加@inject装饰器,明确指定要注入的依赖类型,避免后续Inversify无法解析该依赖项。

3. 确认TypeScript配置有效性

确保tsconfig.json中开启了装饰器相关编译选项:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

内容的提问来源于stack exchange,提问作者khteh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 01:37:20