Webpacker 5.1 breaks recommended TypeScript controller pattern
See original GitHub issueSee: https://github.com/rails/webpacker/issues/2558
Webpacker 5.1 changed the loader for TypeScript, which changed code compilation behavior, which breaks class properties.
So the controller declaration:
I have a simple Stimulus controller, defined to start as follows:
import { Controller } from "stimulus"
export default class extends Controller {
static classes = ["hidden"]
static targets = ["filterInput", "concert"]
hiddenClass: string
concertTargets: HTMLElement[]
filterInputTarget: HTMLInputElement
// and so on
Now breaks with a TypeError: Cannot set property hiddenClass of #<extended> which has only a getter
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:15 (4 by maintainers)
Top Results From Across the Web
5.1 introduces TypeScript property errors (too strict?) #2558
Updating a project from Webpacker 5.0.x to 5.1, and after going through ... Webpacker 5.1 breaks recommended TypeScript controller pattern ...
Read more >Developers - Webpacker 5.1 breaks recommended TypeScript ...
Webpacker 5.1 breaks recommended TypeScript controller pattern.
Read more >Get in Full(stack) Shape With Rails 5.1, Webpacker ... - X-Team
First, we need to set up our back end with Rails. We will follow the usual Model->Controller->View flow. Our app is showing quotes,...
Read more >typescript.md - Webpacker Docs
A webpacker proxy site that pulls in the. ... If you update your App to webpacker >= 5.1 and had TypeScript installed before,...
Read more >Rails 5.1: Awesome Features and Breaking Changes
In this talk, I will cover the improvements brought by Rails 5.1, explain the Core team's motivations behind each feature, and illustrate the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To fix this I removed
['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]
from babel.config.js and added["@babel/plugin-transform-typescript", { 'allExtensions': true, 'isTSX': true, 'allowDeclareFields': true }],
to the top of my plugins entry (anywhere above@babel/plugin-proposal-class-properties
should work).This will also allow the use of declare
declare containerTarget: HTMLDialogElement;
which will future proof your controllers should TS ever align more closely with the ECMA spec.Ran into this issue after upgrading to Stimulus 2.0, running a Rails app with Webpacker 5.2.1. It complains when you use targets in a Stimulus controller built in TypeScript. @fleck’s suggestion fixed it.
In babel.config.js:
In the TypeScript controller: