Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Decorators fail in a Vite and TypeScript 5 project

ErrorA decorator that worked before is rejected by the type checker after moving to TypeScript 5

Short answer

TypeScript 5 has two decorator systems. Without experimentalDecorators it uses the new standard one, which has no parameter decorators and no emitDecoratorMetadata. Libraries written for the old style (class-based DI, ORMs) need experimentalDecorators set to true in the tsconfig that Vite reads.

  • Applies to Vite 7, Vite 8, TypeScript 5.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which tsconfig Vite and the editor use, and whether it sets experimentalDecorators.

    grep -rn "experimentalDecorators\|emitDecoratorMetadata" tsconfig*.json
    npx tsc --version
  2. 2Match what you see, then jump to its fix

    1. 1The decorator function type-checks as wrong, or the library docs say to enable experimentalDecoratorsThe library expects legacy decorators but the flag is offFix
    2. 2tsc is happy but the editor still shows decorator errors, or the reverseThe flag is set in the wrong tsconfigFix
    3. 3Parameter decorators are rejected, or constructor injection gets no type informationParameter decorators or emitDecoratorMetadata with the standard systemFix

What does the error look like?

  • A decorator that worked before is rejected by the type checker after moving to TypeScript 5
  • Parameter decorators (for example on constructor arguments) are not accepted
  • Injected constructor arguments are undefined at runtime

Cause 1 of 3

The library expects legacy decorators but the flag is off

1Check

Without the flag, TypeScript 5 type-checks and emits decorators differently, so a decorator written for the old behaviour usually does not work.

grep -n "experimentalDecorators" tsconfig*.json

2Fix

Enable the option in the tsconfig that covers your app code.

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

3Verify

Run the type check and a build.

npx tsc -b
npx vite build

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

The flag is set in the wrong tsconfig

1Check

Vite respects some options of tsconfig.json for its transform step, and the TypeScript docs describe experimentalDecorators as a compiler option. Check that the file you edited includes the files that use decorators.

cat tsconfig.json
grep -n "include" tsconfig.app.json

2Fix

Put the option in the config whose include covers the decorated files, then restart the TypeScript server in your editor.

{
  "compilerOptions": { "experimentalDecorators": true },
  "include": ["src"]
}

3Verify

The decorator error disappears in the editor and in the build.

npx vite build

Confirmed by vite.dev

Did this fix it?

Cause 3 of 3

Parameter decorators or emitDecoratorMetadata with the standard system

1Check

The new decorator proposal does not allow decorating parameters and is not compatible with emitDecoratorMetadata. Vite's docs also say emitDecoratorMetadata is only partially supported because it needs full type inference.

grep -rn -E "@Inject|constructor\(@" src | head

2Fix

Use experimentalDecorators for that code, where parameter decorators still work, or avoid parameter decorators and metadata-based injection, for example by passing providers explicitly.

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

3Verify

Run the app and check that injected values are defined at runtime, not only that the type check passes.

npx vite

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Read the decorator section of each library you add; it says which system it needs.
  • Keep one decorator style per project. Mixing both is hard to type-check.
  • Do not rely on emitDecoratorMetadata with Vite for dependency injection without testing at runtime.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.