New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental typescript decorator support #14126
Comments
Hi @pi0, I currently have Nuxt 2 with Vitest and I am using decorators in my codebase. For Vitest, I've gotten decorator support via https://www.npmjs.com/package/rollup-plugin-swc3. I have a WIP branch for the Nuxt 3 migration, however my issue is that no decorator metadata is emitted at all. What would be the current (RC12) workaround for getting decorators to work (in Nuxt 3 combined with Vitest)?
It would be great if you could give some pointers on how to enable decorator support as a work around until there is official support, both for Nuxt 3 and testing with Vitest. My {
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
} |
Hi @pi0, Would you be able to comment on this? I just rebased my WIP branch on Nuxt I tried replacing my |
Ok, so in the end it apparently was not that hard, just unknown. TLDR; forget everything, just use @rollup/plugin-typescript:
import typescript from '@rollup/plugin-typescript'
import vue from '@vitejs/plugin-vue2'
import { defineConfig } from 'vite'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
export default defineConfig({
plugins: [typescript(), vue()],
test: {
...
},
})
import typescript from '@rollup/plugin-typescript'
import { defineNuxtConfig } from 'nuxt/config'
export default defineNuxtConfig({
...
// https://vitejs.dev/config/
vite: {
plugins: [typescript()],
},
})
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
} |
For me solution is (i need to use TypeOrm on server): server/tsconfig.json {
"extends": "../.nuxt/tsconfig.server.json",
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
} nuxt.config.ts import rollupPluginTs from '@rollup/plugin-typescript'
export default defineNuxtConfig({
nitro: {
rollupConfig: {
plugins: [
rollupPluginTs(
{
include: ['server/**/*.ts'],
tsconfig: 'server/tsconfig.json'
}
)
]
}
}
}) and it works like charm! |
@misaon Hi, can you share an example repository? I have some vue-tsc errors when adding typeorm into my Nuxt project. |
I am getting a lot of warnings related to nuxt/nitro when I use |
The solution that worked for me was to set export default defineNuxtConfig({
vite: {
esbuild: {
tsconfigRaw: {
compilerOptions: {
experimentalDecorators: true
}
}
}
}
...
}) |
That is not a problem for a while, the problem is that emitDecoratorMetadata not work and is need to use some additional plugins. |
I also experienced issues while developing a module with decorators as class mixins introduce mess - in my case I consistently receive Reflect.metadata error. Tried to enable |
On a recent attempt with this on Nuxt 3.9.0, this solution seems to work for the dev preview, however it crashes when building for production. In the Vite config, setting Note that I had to install import typescript from '@rollup/plugin-typescript';
export default defineNuxtConfig({
vite: {
esbuild: false,
plugins: [typescript()],
},
}); But in turn this breaks typechecking ( I do believe it is wise to just wait for ESBuild to support decorators instead of working it around. I tried to debug the typechecking, and found out that TypeScript tries to throw this error: Traced the part it tries to throw this error and, of course, it's the part where the decorator is used on a class method. The reason it crashes (instead of giving a type error) is because as a result of this error being attempted to be thrown, it cannot assert there's more than 3 arguments (after all, the error message is completely nonsensical in the first place). |
esbuild seems to be considering decorator |
What's the status on TypeScript 5.x decorator support? |
What a mess. Absolutely nothing worked for me. |
ESBuild seems planning to support decorators (evanw/esbuild#104) but it is not landed as the spec is experimental itself.
We shall use SWC to have experimental support for decorators (vitest-dev/vitest#708 (comment)).
Related feature request for server part (nitro): unjs/nitro#273
The text was updated successfully, but these errors were encountered: