Skip to content

add helpers for private-named instance fields #77

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

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tslib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export declare function __asyncValues(o: any): any;
export declare function __makeTemplateObject(cooked: string[], raw: string[]): TemplateStringsArray;
export declare function __importStar<T>(mod: T): T;
export declare function __importDefault<T>(mod: T): T | { default: T };
export declare function __classPrivateFieldGet(receiver: any, privateMap: WeakMap<any, any>): any;
export declare function __classPrivateFieldSet(receiver: any, privateMap: WeakMap<any, any>, value: any): any;
Copy link
Author

@Neuroboy23 Neuroboy23 Sep 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these function declarations be genericized? My instinct is that they should, but I'm not sure which generic form would be appropriate, given the lack of generics in other function declarations in this file.

Each of these methods returns a value of a known type. That would make me want to write the following.

export declare function __classPrivateFieldGet<T>(receiver: any, privateMap: WeakMap<any, T>): T;
export declare function __classPrivateFieldSet<T>(receiver: any, privateMap: WeakMap<any, T>, value: T): T;

However, I don't understand why WeakMap<any, ...> is allowed, given that JS WeakMap.get/set only accept an object as the first parameter. The following TypeScript compiles fine but fails at runtime.

// typescript
const m: WeakMap<any, any> = new WeakMap<any, any>();
m.set(false, true);
// emitted javascript
const m = new WeakMap();
m.set(false, true); // fails at runtime

I would have expected TS's definition to be WeakMap<K extends object, T>. If that were the case, I believe the generic form would be something like the following

export declare function __classPrivateFieldGet<K extends object, T>(receiver: K, privateMap: WeakMap<K, T>): T;
export declare function __classPrivateFieldSet<K extends object, T>(receiver: K, privateMap: WeakMap<K, T>, value: T): T;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they should be generic, so I’m doing that in #82.

15 changes: 15 additions & 0 deletions tslib.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,18 @@ export function __importStar(mod) {
export function __importDefault(mod) {
return (mod && mod.__esModule) ? mod : { default: mod };
}

export function __classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}

export function __classPrivateFieldSet(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
19 changes: 19 additions & 0 deletions tslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var __asyncValues;
var __makeTemplateObject;
var __importStar;
var __importDefault;
var __classPrivateFieldGet;
var __classPrivateFieldSet;
(function (factory) {
var root = typeof global === "object" ? global : typeof self === "object" ? self : typeof this === "object" ? this : {};
if (typeof define === "function" && define.amd) {
Expand Down Expand Up @@ -234,6 +236,21 @@ var __importDefault;
return (mod && mod.__esModule) ? mod : { "default": mod };
};

__classPrivateFieldGet = function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};

__classPrivateFieldSet = function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}

exporter("__extends", __extends);
exporter("__assign", __assign);
exporter("__rest", __rest);
Expand All @@ -254,4 +271,6 @@ var __importDefault;
exporter("__makeTemplateObject", __makeTemplateObject);
exporter("__importStar", __importStar);
exporter("__importDefault", __importDefault);
exporter("__classPrivateFieldGet", __classPrivateFieldGet);
exporter("__classPrivateFieldSet", __classPrivateFieldSet);
});