question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpacker 5.1 breaks recommended TypeScript controller pattern

See original GitHub issue

See: 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
fleckcommented, Jul 3, 2020

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.

4reactions
szTheorycommented, Dec 15, 2020

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:

    plugins: [
      "babel-plugin-macros",
      "@babel/plugin-syntax-dynamic-import",
      isTestEnv && "babel-plugin-dynamic-import-node",
      "@babel/plugin-transform-destructuring",
      // added this
      [
        "@babel/plugin-transform-typescript",
        { allExtensions: true, isTSX: true, allowDeclareFields: true },
      ],

In the TypeScript controller:

export default class extends Controller {
  static targets = ["addItem", "template"];

  // TypeScript with Stimulus 2.0 must declare properties
  // https://github.com/stimulusjs/stimulus/issues/303#issuecomment-653630360
  declare templateTarget: HTMLElement;
  declare addItemTarget: HTMLElement;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found