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

Add support for declare syntax for TS class fields #536

Closed
alangpierce opened this issue May 18, 2020 · 0 comments · Fixed by #537
Closed

Add support for declare syntax for TS class fields #536

alangpierce opened this issue May 18, 2020 · 0 comments · Fixed by #537

Comments

@alangpierce
Copy link
Owner

See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier . As part of switching to fully spec-compliant class fields, a TS project will need to look for non-initialized class fields and make sure that the behavior is correct when using define. For example, with this code:

class A {
  foo: number | string = 3;
}

class B extends A {
  foo: string;
}

TypeScript with useDefineForClassFields: true gives the error TS2612: Property 'foo' will overwrite the base property in 'A'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.

The typical fix is to change the class definition to:

class B extends A {
  declare foo: string;
}

But Sucrase currently fails to parse the declare syntax, so that's an important prerequisite step.

alangpierce added a commit that referenced this issue May 18, 2020
Fixes #536

We already completely remove uninitialized class fields for now, which agrees
with the `declare` behavior, so the only change needed here was to parse the
syntax without crashing. In the future, when we remove the class fields
transform, we'll need to distinguish declare vs non-declare fields.
alangpierce added a commit that referenced this issue May 18, 2020
Fixes #536

We already completely remove uninitialized class fields for now, which agrees
with the `declare` behavior, so the only change needed here was to parse the
syntax without crashing. In the future, when we remove the class fields
transform, we'll need to distinguish declare vs non-declare fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant