Inversify + Typescript 5: TS1239: Unable to resolve signature of parameter decorator when called as an expression

2.6k Views Asked by At

One of my projects uses Inversify for dependency injection like this:

@injectable()
export class MyClass {

constructor(
    @inject(OtherClass) private otherClass: OtherClass,
    ...
) {
}

This always worked fine and should be OK according to the docs, but when trying to upgrade from TS4 to TS5 it results in the following error (which refers to the line with constructor(...)):

error TS1239: Unable to resolve signature of parameter decorator when called as an expression.

Is there anything that can be done about this or is Inversify no longer usable with TS5? If it's no longer compatible, would there be a drop-in replacement for Inversify?

3

There are 3 best solutions below

1
Ana Teixeira On BEST ANSWER

Maybe it's related with your inversify version.. [email protected] seems to work fine

But as you can see there is changes in TS5 regarding this matter, More Accurate Type-Checking for Parameter Decorators in Constructors Under --experimentalDecorators

keep it strong

1
Gary Archer On

I didn't realise TypeScript 5 was out, but after seeing your question I upgraded an inversify project of mine and the code worked the same as previously. Eg this class of mine is similar to yours:

@injectable()
export class CompanyService {

    private readonly _repository: CompanyRepository;
    private readonly _customClaims: SampleCustomClaims;

    public constructor(
        @inject(SAMPLETYPES.CompanyRepository) repository: CompanyRepository,
        @inject(BASETYPES.CustomClaims) customClaims: CustomClaims) {

        this._repository = repository;
        this._customClaims = customClaims as SampleCustomClaims;
    }

In my case I use the following tsconfig.json file. I have always needed to use the experimentalDecorators setting, which still works in TS5:

{
  "compilerOptions": {
    "strict": true,
    "target": "ES2022",
    "lib": ["ES2022"],
    "module":"ES2022",
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "outDir": "dist",
    "sourceMap": true
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

So it feels like they remain compatible and you should be able to get your project working by comparing to my repo. There could be other valid setups that are different to my own though.

0
Dragos Podaru On

To add some informations to this, maybe just to clarify why this is working. inversify works with typescript 5 or higher ( as long as you have experimentalDecorators ) because when I prepared the upgrade we made sure to include string | symbol | undefined as types for keys, since this "breaking" change was somehow unavoidable. Hope this clarifies a bit what's really happening.