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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With