next.js: Decorators don't work in with-typescript example.
Examples bug report
Example name
with-typescript
Describe the bug
If I start using decorators, next.js throws an error to ask to add @babel/plugin-proposal-decorators.
And, it throws same error even after I add the plugin.
Syntax Error:
6 | }
7 |
> 8 | @myDecorator
| ^
9 | export default class IndexPage extends React.Component{
10 | render () {
11 | return <div>
Add @babel/plugin-proposal-decorators (https://git.io/vb4ST) to the 'plugins' section of your Babel config to enable transformation
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Set
"experimentalDecorators": trueto tsconfig.json - Add some any decorators to any page
import React from 'react'
import Link from 'next/link'
function myDecorator (constructor: any) {
}
@myDecorator
export default class IndexPage extends React.Component{
render () {
return <div>
Hello World.{' '}
<Link href="/about">
<a>About</a>
</Link>
</div>
}
}
Expected behavior
It should work with decorators.
Screenshots
If applicable, add screenshots to help explain your problem.
System information
"dependencies": {
"@zeit/next-typescript": "^1.1.0",
"next": "^6.1.1",
"react": "^16.4.1",
"react-dom": "^16.4.1"
},
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.0.0-beta.51",
"@types/next": "^2.4.7",
"@types/react": "^16.0.36"
}
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 13
- Comments: 24 (8 by maintainers)
Commits related to this issue
- updated nextjs configuration, but still having decorator related issue - https://github.com/vercel/next.js/issues/4707 — committed to gridaco/assistant by softmarshmallow 6 years ago
After adding
@babel/plugin-proposal-decoratorsand@babel/plugin-proposal-class-properties, I still got the following error:Solved by installing and using
babel-plugin-transform-typescript-metadata:npm install --dev --save babel-plugin-transform-typescript-metadata.babelrc
.tsconfig
Having the same issue. Have you found any solution yet?
Edit: Fixed it
Had to add additional plugin for parameter decorators. 👉🏻 LINK
Here is my:
Despite having the same config as yours @denniske, I’m still having issues: I use type-graphql, when using Arg decorator, I get:
Any ideas?
@timneutkens
I cloned the latest
with-typescriptrepo & added those 2 settings to.babelrc, but I’m still getting those errors.https://github.com/webberwang/next.js/commit/ed1a37ff865ed101e9fefb91ee9dafa04e69c9d7
Thanks, @aditya063 for the code, I got it working with Next v10 using the
.babelrcbelow:Repo here: https://github.com/chhschou/nextjs-graphql-serverless-starter
I had similar problem as here: https://github.com/kriasoft/react-starter-kit/issues/1522
The solution was to check what babel version is used by next, and use the same version in
@babel/plugin-proposal-decoratorsStill getting errors here w/ Next version
10.0.4. Tried @aditya063’s exact solution and I get the error messageWould really love to use TypeGraphQL/TypeORM with Apollo Server via a Next API Route, and have it all in one project.
Hoping this gets supported soon!
It seems to work if I add those two plugins below.
But, I assume those two plugins will be deprecated in babel v7. Therefore, we need to use
@babel/plugin-proposal-decoratorseventually.But, It starts breaking again after I tried like the below.
Then throws this error:
Fortunately, it works again after I install thew newest version of babel(v7.0.0-beta.51).
Nope, my guess (could be wrong) is that the NextJS team may have to explicitly enable
plugin-proposal-decoratorsas an option in the Babel config: https://github.com/vercel/next.js/tree/canary/packages/next/bundles/babel/packagesIn the meantime I switched to Serverless framework. Would love to eventually run this off NextJS API routes though due to easy preview deployments and same-origin.
@webberwang The problem is that neither
@babel/plugin-proposal-decoratorsnor@babel/plugin-proposal-class-propertiessupport the function parameter decorator.This
instead of
will work well.
There is a babel plugin for the parameter decorator: https://github.com/WarnerHooh/babel-plugin-parameter-decorator. I’ve applied it to use the parameter decorator on nextjs but failed to remove all error messages 😦
Check here @alejomendoza
https://github.com/BoostIO/Boostwiki/blob/master/package.json#L68-L69
https://github.com/BoostIO/Boostwiki/blob/master/.babelrc
I still get an error when trying to import a typescript module with a decorator @Rokt33r, any ideas on how to make this work? thanks!
I’m on next
7.0.0-canary.0