Skip to content
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

Changed type of the derived class property makes it undefined #9179

Closed
vyshkov opened this issue Jul 8, 2024 · 3 comments
Closed

Changed type of the derived class property makes it undefined #9179

vyshkov opened this issue Jul 8, 2024 · 3 comments
Labels
Milestone

Comments

@vyshkov
Copy link

vyshkov commented Jul 8, 2024

Describe the bug

If I redefine property type to be more specific in the derived class, it makes it undefined.

    class A {
      prop: string;

      constructor(test: string) {
        this.prop = test;
      }
    }

    class B extends A {
      prop: 'str1' | 'str2';

      constructor(test: 'str1' | 'str2') {
        super(test);
      }
    }

    const test = new B('str2');

    console.log(test.prop);

I know, I didn't explicitly set it in the constructor of class B, however, when I compile the file with TSC it works differently: I can access the prop and it's not undefined.

Input code

No response

Config

{
  jsc: {
    parser: {
      syntax: 'typescript',
      dynamicImport: true,
      decorators: true
    },
    target: 'es2021',
    transform: {
      decoratorMetadata: true,
      legacyDecorator: true
    },
    baseUrl: path.resolve(__dirname, '.')
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.6.7&code=H4sIAAAAAAAAA32PywrCMBBF94X%2Bw92l2RR0aXBh%2F0TiUAshCUmKQu2%2Fm4fBB%2BqshsmdMycAINXRexywtA1yWWfsDj64SY%2BibepYGh1nswzGdYF8qBH%2B3ATCefJ9AmCPFBL1aS3NWnnl6gC6BtKnL%2FdZpG8YbrnZsr8iH9k3Iz9bKjn%2BWyYRs2%2FU1nTB0D1I4jViFPXKjBmWP8nFHeglkztBAQAA&config=H4sIAAAAAAAAA1VPOw7DIAzdOQXy3KHN0KF36CEQdSIqfsKOVBTl7oUE0maz38fveRFSwps0PORSxrJElQjTsReEsmf1KQhwjkg6mchw6SxTpUZlCTdo3RlglSbk6kIarsOtOcCGQNgdDXPGmzH%2FZ%2BrgYkKis7BKlZ8snhNFSwUXXvNGtl9q373BHX6iHnYcBkPP7uQ0o1i%2FsnkCbBcBAAA%3D

SWC Info output

No response

Expected behavior

this.prop should be defined

Actual behavior

No response

Version

1.6.6

Additional context

No response

@vyshkov vyshkov added the C-bug label Jul 8, 2024
@kdy1
Copy link
Member

kdy1 commented Jul 9, 2024

#7435

It looks like a different issue?

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
@kdy1 kdy1 reopened this Jul 9, 2024
@kdy1 kdy1 added this to the Planned milestone Jul 9, 2024
@magic-akari
Copy link
Member

You should set useDefineForClassFields.

@kdy1 kdy1 modified the milestones: Planned, v1.7.0 Jul 17, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Aug 17, 2024

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants