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

add helpers for private-named instance fields #77

Merged
merged 1 commit into from
Dec 27, 2019

Conversation

Neuroboy23
Copy link

A companion to microsoft/TypeScript#30829, this PR adds the helper methods for private-named instance fields into tslib.

The original helper methods were written by @joeywatts and @mheiber in the aforementioned PR. They have simply been copied and exposed here.

@@ -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.

@Neuroboy23
Copy link
Author

Requesting a review from @rbuckton.

@mheiber
Copy link

mheiber commented Dec 23, 2019

for reviewers: this PR is still up-to-date with microsoft/TypeScript#30829

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

Successfully merging this pull request may close these issues.

5 participants