Decorators fail in a Vite and TypeScript 5 project
A decorator that worked before is rejected by the type checker after moving to TypeScript 5Short 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.
Fix it fast
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 --version2Match what you see, then jump to its fix
- 1
The decorator function type-checks as wrong, or the library docs say to enable experimentalDecoratorsThe library expects legacy decorators but the flag is offFix - 2
tsc is happy but the editor still shows decorator errors, or the reverseThe flag is set in the wrong tsconfigFix - 3
Parameter decorators are rejected, or constructor injection gets no type informationParameter decorators or emitDecoratorMetadata with the standard systemFix
- 1
What does the error look like?
A decorator that worked before is rejected by the type checker after moving to TypeScript 5Parameter decorators (for example on constructor arguments) are not acceptedInjected 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*.json2Fix
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 buildConfirmed by typescriptlang.org
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.json2Fix
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 buildConfirmed by vite.dev
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 | head2Fix
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 viteConfirmed by vite.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
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 fixWeb apps
Cypress refuses to start with 'Your configFile is invalid'
Your configFile threw an error from: /path/to/cypress.config.ts
Cypress could not load cypress.config.js or .ts, or found something wrong in it.
5 causes, each with a fixWeb apps
ESLint cannot decide which copy of the @typescript-eslint plugin to use
The message lists two file paths for the same plugin, each with the name of the config that loaded it
ESLint found the same plugin at two different file paths and will not pick one.
3 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint was configured to run using parserOptions.project, but the file is not in the TSConfig
However, that TSConfig does not include this file
Type-aware linting needs every linted file to belong to a TypeScript project.
4 causes, each with a fixWeb apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fix