TypeScript: Fail to import `classPrivateFieldGet` from tslib when `"importHelpers": true`

I am testing out the lastest ECMA private fields. I get an error when importHelpers enabled:

This syntax requires an imported helper named '__classPrivateFieldGet' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.ts(2343)

I’m not sure if there’s pending work on the tslib, so I’m repoting this anyway.

TypeScript Version: 3.8.1-rc TS Lib Version: 1.1.0

Search Terms: tslib classPrivateFieldGet

Code

class A {
    #a: number

    constructor() {
        this.#a = 1
    }
}


Expected behavior:

This should compile

Actual behavior:

Shows an error on tslib

Playground Link:

Related Issues:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 22 (6 by maintainers)

Most upvoted comments

Same issue with TS 3.9.3 and tslib 2.0.0

With Typescript 3.8.2 (also applies with TS 3.9.3) and tslib 2.0.0, I have got a runtime Error ReferenceError: __classPrivateFieldGet is not defined

I’m experiencing this on 4.8.4 but slightly different error, not sure if also caused by importHelpers

web: > node main
web: /var/app/current/node_modules/tslib/tslib.js:250
web: return privateMap.get(receiver);
web: ^
web: TypeError: privateMap.get is not a function
web: at Object.__classPrivateFieldGet (/var/app/current/node_modules/tslib/tslib.js:250:27)

Same issue here with typescript 3.8.3 and tslib 1.11.1.

I have a few additional observations:

  1. When I only compile one file typescript seems to completely ignore the importHelpers: true option and emit all required helpers (including __classPrivateFieldGet and __classPrivateFieldSet) into the js file. When the compiled file references other files via imports the behaviour @pirix-gh described occurres.
  2. When module: "CommonJS" is set, it imports all helpers just as you would expect. However, when module: "ESNext" or "module: "ES2015" it does not import __classPrivateFieldGet and __classPrivateFieldSet (other helpers like __awaiter are imported though)

@boy51 getting the same issue, i don’t have importHelpers though, it looks like it’s coming from my p-queue package since it’s using private class fields, have you found a workaround?

Edit: It seems installing tslib^2.4.1 manually has fixed the issue for me.

Just add tslib^2.4.1 to resolutions, And run npm install, the lock file will update, remove tslib^2.4.1 on resolutions just added, run npm install, again, the lock file will be updated again, and this problem will be fixed

This is again a bug in 4.8.4. Easy to reproduce:

mkdir test && cd test
npm init
tsc --init
npm i -D typescript

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "importHelpers": true, // <-- CULPRIT
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": false
  }
}

index.ts

export class Test {
  #pvt: string;

  constructor(pvt: string) {
    this.#pvt = pvt;
  }
}
image