What's the purpose of #private in TypeScript definition files?

Viewed 167

In TypeScript 4.0.5, when transpiling a class that uses ECMAScript private fields, like this one:

export class Test {
    #foo = 'foo';
    #bar = 6;

    concat() {
        return this.#foo + this.#bar;
    }
}

The result is this:

var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to get private field on non-instance");
    }
    return privateMap.get(receiver);
};
var _foo, _bar;
export class Test {
    constructor() {
        _foo.set(this, 'lol');
        _bar.set(this, 6);
    }
    concat() {
        return __classPrivateFieldGet(this, _foo) + __classPrivateFieldGet(this, _bar);
    }
}
_foo = new WeakMap(), _bar = new WeakMap();

And the definition file is this:

export declare class Test {
    #private;
    concat(): string;
}

What's the purpose of #private here? It just removes TypeScript backwards compatibility. The # character will make the parsing fail in older TypeScript versions, and it doesn't bring anything in the IDE:

Attempt to access #foo

And when we try to use the #private field, it doesn't show any useful information:

Attempt to access #private

I have found the corresponding commit here: https://github.com/microsoft/TypeScript/pull/30829/commits/3f8b49012636a365762598847a463f1cf189a59f but neither the comment nor the commit message helped me understand.

0 Answers
Related