Node.JS inversify constructor injection with @unmanaged

547 views Asked by At
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>, db: Database) {
        this._db = db;
        this._repository = this._db.getRepository(entity);
    }

Build error:

Data/Repositories/RepositoryBase.ts:11:18 - error TS1239: Unable to resolve signature of parameter decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'string'.

11     constructor(@unmanaged() entity: EntityTarget<T>, db: Database) {
                    ~~~~~~~~~~~


Found 1 error in Data/Repositories/RepositoryBase.ts:11
2

There are 2 answers

0
Vladyslav Zuiev On

The issue is fixed.

You can simply update to inversify v6.0.2 and up (if exists)

0
Luke On

To save people looking around for a solution. The issue seems to be caused by Typescript 5, so you can downgrade Typescript.

A work around while waiting for the pull request would be to change the type:

import { unmanaged as _unmanaged } from "inversify";

// TODO: Remove after https://github.com/inversify/InversifyJS/issues/1505 is resolved
export const unmanaged = _unmanaged as () => (target: DecoratorTarget, targetKey: string | undefined, index: number) => void;

Here is the your issue: https://github.com/inversify/InversifyJS/issues/1505

Here is a potential pull request: https://github.com/inversify/InversifyJS/pull/1499