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.

Parameter properties of imported TypeScript classes don't get set

See original GitHub issue

Describe the bug Parameter properties of classes defined in TypeScript modules are accepted as valid syntax but don’t get set as expected. Instead, they remain undefined.

To Reproduce

File foo.stories.tsx:

import {Foo} from "./Foo";

const f = new Foo("foo");

File foo.tsx:

export class Foo {
    constructor(public state: string) {
        console.log("State before:", this.state);
        this.state = state;
        console.log("State after:", this.state);
    }
}

Expected behavior The console output should read

State before: "foo"
State after: "foo"

but in reality it is

State before: undefined
State after: "foo"

FWIW, if I don’t set this.state manually like I do above, it will remain undefined. (I.e., f.state in the story will also be undefined.) Also, if I move the class from foo.tsx to foo.stories.tsx, the issue will go away.

System

$ npx sb@next info

Environment Info:

  System:
    OS: Linux 5.8 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
  Binaries:
    Node: 14.15.4 - /usr/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.10 - /usr/bin/npm
  Browsers:
    Chrome: 88.0.4324.96
    Firefox: 85.0
  npmPackages:
    @storybook/addon-actions: ^6.1.16 => 6.1.16 
    @storybook/addon-essentials: ^6.1.16 => 6.1.16 
    @storybook/addon-links: ^6.1.16 => 6.1.16 
    @storybook/react: ^6.1.16 => 6.1.16 

Additional context This issue looks somewhat similar – maybe we’re looking at same cause here?

Post scriptum I’m new to Storybook, so I’m not sure what the state of the TypeScript support is. (This issue is saying that “Storybook is not meant to support TypeScript”, whereas the docs state that

Storybook has built-in Typescript support, so your Typescript project should work with zero configuration needed.

So what is it?

Also, as I don’t want to run into similar issues in the future, can I somehow configure Storybook to use the TS compiler instead of Babel?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:16
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
fingerarturcommented, Jul 14, 2021

The issue is still present in "@storybook/react": "^6.3.4" would you consider increasing the priority on this? It can cause big problems for anybody using typescript.

PS: @duarten Great job with your workaround, thank you.

13reactions
duartencommented, Apr 10, 2021

@Alecell you could filter the plugins array, but it’s probably simpler to just add the @babel/plugin-transform-typescript to the plugins, which is what I’m doing:

// main.js
babel: async options => ({
    ...options,
    presets: [
        ["@babel/preset-env", { shippedProposals: true }],
        "@babel/preset-typescript",
        ["@babel/preset-react", { runtime: "automatic" }],
    ],
    plugins: ["@babel/plugin-transform-typescript", ...options.plugins],
}),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Classes - TypeScript
If get exists but no set , the property is automatically readonly; If the type of the setter parameter is not specified, it...
Read more >
How To Use Classes in TypeScript | DigitalOcean
This way you do not need to set the property of the class to the value of the parameter received in the constructor,...
Read more >
Looking for a reason the typescript class will not recognize ...
For backwards compatibility reasons, the TypeScript compiler does not turn on --strict by default, so it could also be that the tutorial project ......
Read more >
typescript-cheatsheet - GitHub Pages
Conversely, to consume a variable, function, class, interface, etc. exported from a different module, it has to be imported using one of the...
Read more >
What's the problem with TypeScript's classes? - Medium
TypeScript handles values — things that will be present at runtime — and types ... and you don't want to set the attribute...
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