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

TS: Class fields that use declare cannot be initialized #2380

Closed
marvinhagemeister opened this issue Jul 9, 2022 · 1 comment
Closed

TS: Class fields that use declare cannot be initialized #2380

marvinhagemeister opened this issue Jul 9, 2022 · 1 comment

Comments

@marvinhagemeister
Copy link

Came across this pattern in the wild which throws an error with esbuild but passes compilation with tsc:

declare const isOptional: unique symbol;

class Foo {
  public declare readonly [isOptional] = true;
}

// TSC detects "a" as being true
const a = new Foo()[isOptional]

The error thrown by esbuild:

✘ [ERROR] Class fields that use "declare" cannot be initialized

    src/parser/v2/test2.ts:4:42:
      4 │   public declare readonly [isOptional] = true;
@evanw
Copy link
Owner

evanw commented Jul 10, 2022

Huh weird, TIL. It looks like this is a very narrow special case (you can't even set it to null). I'll just allow anything here and let the type checker enforce the special cases.

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

No branches or pull requests

2 participants