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

Fix class properties declarations #16386

Closed
filipsobol opened this issue May 20, 2024 · 1 comment · Fixed by #16318 or Klantinteractie-Servicesysteem/KISS-frontend#929
Closed

Fix class properties declarations #16386

filipsobol opened this issue May 20, 2024 · 1 comment · Fixed by #16318 or Klantinteractie-Servicesysteem/KISS-frontend#929
Labels
squad:core Issue to be handled by the Core team. type:improvement This issue reports a possible enhancement of an existing feature.

Comments

@filipsobol
Copy link
Member

As part of #16292, we plan to update our codebase to transpile to a valid ES2022 code (but still target es2019 in TypeScript for the time being). To catch bugs early, we should also update esbuild (which we use for our test environment) to the latest version, which was blocked by the issues described in #14703.

Let's take this code as an example:

export class Test {
  public test?: string;
}

TypeScript and [email protected] (which we currently use) will transpile this to the following:

export class Test {
}

However, the newer versions of esbuild and swc will transpile the same code to the following if the target is set to ES2021 or lower:

export class Test {
  constructor() {
    _setPublicField(this, "test"); // `_setPublicField` removed for clarity
  }
}

When the target is set to ES2022, it's transpiled to:

export class Test {
  test;
}

Unlike the first case, Test.prototype.test is declared when a new instance of Test is created. This is a problem in places where we are checking for the existence of a class property.

This can be fixed by using Typescript's declare keyword, which tells it that the given property exists and what type it is, but doesn't add the property to the class.

Updated source code:

export class Test {
  public declare test?: string; // <--- Notice the `declare` keyword
}

Output:

export class Test {
}
@filipsobol filipsobol added the type:improvement This issue reports a possible enhancement of an existing feature. label May 20, 2024
@filipsobol
Copy link
Member Author

Done in #16318.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
squad:core Issue to be handled by the Core team. type:improvement This issue reports a possible enhancement of an existing feature.
Projects
None yet
2 participants