user11972302
user11972302

Reputation:

How can I use private fields in TypeScript?

When I try to run the following code that uses a private field, I get an "Invalid character" error at the position of #.

class MyClass {
    #x =  10;
}

This is my tsconfig.json:

{
    "compilerOptions": {
        "target": "esnext",
    …
}

I get this error:

2:5 - error TS1127: Invalid character.

Why is that, and how can I fix it?

Upvotes: 0

Views: 1204

Answers (1)

Nenad
Nenad

Reputation: 26617

You still have to use private keyword:

class MyClass {
    private x =  10;
}

Implement ES Private Fields is on the TypeScript roadmap in the Future section (so, I guess, earliest in version 3.7).

Upvotes: 3

Related Questions