| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Sorry, something went wrong.
|
Amazing achievement! Are the helpers _classPrivateFieldGet/_classPrivateFieldSet treated in the same way as __extends, __awaiter, __generator? (that would include modifications to tslib) |
Sorry, something went wrong.
|
mihailik thanks! As far as I know, the helpers work the same way. Is any additional work required to get the helpers into tslib? I was hoping the syncing was semi-automated. |
Sorry, something went wrong.
Sadly, they are not. A paired PR against tslib with the finalized helpers will be needed. |
Sorry, something went wrong.
|
Wesley Wigham (@weswigham): I have asked our IP team to set up a fork of the tslib repo. Joey Watts (@joeywatts) and Max Heiber (@mheiber): Will you take a look at this other repo? I think it will be an easy mod. |
Sorry, something went wrong.
|
Forgive me if this is obvious, but I've look around on the issues and couldn't see this covered anywhere: Is there any particular reason why we can't do private #bar = 3;? Would there be any chance of getting that syntax supported? In my eyes it'd give us the best of both worlds, as then all class properties can maintain their indentation level, along with making refactoring easy for people like me who prefix all our private properties with _. I mean you could pretty much just use a global find-and-replace using /private #/ regex, since # is only valid for private class properties - the only issue with this I can think of would be with strings. Also, could someone confirm for me if its planned for TS somewhere down the line to transform private to #? (I suspect it is, but again there's a lot of heavy comments, PRs, & issues on the matter, so it'd be nice to have a simple one-liner comment about it if possible). |
Sorry, something went wrong.
|
Gareth Jones (@G-Rath) no, no chance of it being supported by JS itself (see the FAQ). I suspect/hope TS would not be likely to support non-type-related syntax that isn't standard to JS. |
Sorry, something went wrong.
|
Jordan Harband (@ljharb) fair enough - so then I assume TS is aiming to deprecate & remove the private keyword? (over time of course) |
Sorry, something went wrong.
|
Gareth Jones (@G-Rath) Your question on the future of TypeScript's keyword private is one that many people will have, and may provoke more discussion. Would you like to raise it as a new top-level issue? |
Sorry, something went wrong.
|
Rob Palmer (@robpalme) more than happy to - done via #31670 |
Sorry, something went wrong.
|
"private field" decorator "private" only? class Sample {
private #field: number = 1;
} |
Sorry, something went wrong.
|
Ron Buckton (@rbuckton) has re-landed the refactoring of class properties. The next step is to rebase this PR. |
Sorry, something went wrong.
|
Does private name use same private fields transformation of #name? |
Sorry, something went wrong.
|
L&H (@Aqours) no, private is meant to be strictly design-time. |
Sorry, something went wrong.
|
The rebase is in progress and we're expecting to update soon! |
Sorry, something went wrong.
|
I noticed an issue with our Language Service changes. Type inference and red underlines work correctly, but autocomplete is borked. Steps to reproduce:In a class with private field #foo#, start typing this.#foo Actual behavior:Autocomplete completes incorrectly (see screenshot). It is actually completing with secret variables we use in the transformation Expected behavior:Autocomplete completes as #foo. Advice welcome on how to fix autocomplete in this PR! |
Sorry, something went wrong.
|
That is actually really weird. Are you sure that it's not just picking up the output .js files? Are you able to reproduce in a fourslash test? |
Sorry, something went wrong.
|
Thanks for your suggestion, Daniel Rosenwasser (@DanielRosenwasser): the completions were picking up the JS output. I added an export to let TS know the file is a module, and no longer see weird completions. |
Sorry, something went wrong.
|
fwiw, I agree that the hack is a bad idea. Wouldn't want the hack to be a secret, though, since it's good to know that the privacy of the transformed code is not absolute, barring control over the WeakMap prototype. Daniel Ehrenberg (@littledan), are there other privacy hills privacy holes, as well? |
Sorry, something went wrong.
|
Max Heiber (@mheiber) What do you mean by privacy hills? |
Sorry, something went wrong.
|
I meant "privacy holes." Autocorrect got me! |
Sorry, something went wrong.
|
I'm not aware of any privacy holes in the main design. There may be other hacks that just work on this transform, though. |
Sorry, something went wrong.
The feature is pretty popular, I imagine a lot of places won't adopt private fields primarily for this reason. Has a similar feature been considered for proposing at TC39? e.g.: class Point {
constructor(#x: number, #y: number) {}
}And for public fields maybe allow using this.prop as a parameter (or destructured parameter) e.g.: class Image {
constructor(
this.data: ImageData, // Simple parameter
{
height: this.height,
width: this.width /* in destructuring */
}: { width: number, height: number },
) {}
} |
Sorry, something went wrong.
|
Given that the __classPrivateFieldGet and __classPrivateFieldSet could be customized in the way they work by implementing them differently and importing that customized tslib, wouldn't it make sense to also put the _nameWeakMap_1 = new WeakMap(); map initialization into its own tslib helper function? This would allow to implement a version which also works without WeakMap support (e.g. IE <= 10), or for anyone who wants to use a different mechanism, such as with non-enumerable properties or Symbol-based private fields (knowing that these are not private as per tc39 proposal, but may perform better). |
Sorry, something went wrong.
|
In case anyone else is wondering the same thing I was, it looks like the first production release to include this will be version 3.8, scheduled for sometime in February: |
Sorry, something went wrong.
|
This looks ridiculous and disrupt developer experience with the language, especially for newers) Please, don't do it. This is feature for Typescript 4, not for 3.8, where we can break backward compatibility. Or, at least either "private" or "#" must be deleted in Typescript 4. |
Sorry, something went wrong.
|
Nikolai Kulikov (@kokushkin) Obviously # should not be dropped in TypeScript 4 since it's already an ECMAScript standard (still stage 3, but advancing to stage 4 is just a formality at this point in the process). And I think dropping private would be premature since it would break backward compatibility and there is currently no other way to create compile-time only private properties (or at least properties that are accessible somehow outside the class, e.g. symbols). Anyway, this PR isn't the best place for these discussions...just thought I would briefly give my two cents in response to this. |
Sorry, something went wrong.
This is not true. See my previous comment. |
Sorry, something went wrong.
|
HE Shi-Jun (@hax) You're right, I didn't mean to mislead anyone, just should have written more carefully... I was trying to emphasize that this isn't just an early-stage proposal that TypeScript decided to implement on their own. I should have said it's a proposed standard that's very near the final stage of the standardization process, with private fields now enabled by default in Chrome, Firefox, and node. |
Sorry, something went wrong.
Maybe you meant s/Firefix/Edge/? Firefox has public instance fields but no real support for private fields yet. Though it looks like it's just a matter of time until they get to it: https://bugzilla.mozilla.org/show_bug.cgi?id=1562054. But >50% of web users have private fields enabled so your overall point stands I think. :) |
Sorry, something went wrong.
|
@jkrems Ah yes, thanks for the correction. The public fields part of the proposal is enabled by default in Firefox...next step will probably be to add private fields behind a flag. Implementation status is periodically updated here: |
Sorry, something went wrong.
|
A question about the downlevel emit - to use this feature I have to target ES2015. But that will mean classes remain as classes, which breaks IE11. And yet IE11 supports WeakMap to some extent. And I find that TS does actually produce what looks like a good emit private fields for ES5, but gives me an error message. Is IE11's WeakMap good enough for this usage? It returns undefined from set but the TS emit here doesn't care about that. If I could suppress the error somehow... #29950 |
Sorry, something went wrong.
…e fixes Summary: * Add private-named instance fields. Ex: `x.#name;`. [1] * Support type-only imports and exports. [2] * Improve the detection of conditional expressions `a ? b : c`. Allow multiple lines. * Add rules of round brackets `()` to correct the highlighting of pairs of brackets. [3] [1] microsoft/TypeScript#30829 [2] microsoft/TypeScript#35200 [3] https://unix.stackexchange.com/questions/527268/kate-18-12-3-no-longer-shows-matching-parenthesis-for-typescript Reviewers: #framework_syntax_highlighting, dhaumann, cullmann Reviewed By: #framework_syntax_highlighting, cullmann Subscribers: kwrite-devel, kde-frameworks-devel Tags: #kate, #frameworks Differential Revision: https://phabricator.kde.org/D27692
|
The implementation could use a single WeakMap per module, instead of one WeakMap per property. It think it may be more efficient. Maybe I missed it: is there a specific reason it needs to be one WM per property? |
Sorry, something went wrong.
|
At the least you’d need one for statics, and one for instances - but actually, it wouldn’t necessarily be more efficient, since you’d need a containing object to look up the individual fields in - and for functions, you’d have to be very careful not to expose the containing object as the receiver. |
Sorry, something went wrong.
|
Ah, right! I take that back, because for each instance, we'd need that accompanying object, which means O(n) instead of O(1) mem use for storage containers for the key-value pairs, where n is number of class instances and 1 is the constant number of defined private fields. I overlooked that. |
Sorry, something went wrong.
|
Joe Pea (@trusktr) FYI, there's also this issue: https://github.com/tc39/proposal-class-fields/blob/master/PRIVATE_SYNTAX_FAQ.md#how-can-you-model-encapsulation-using-weakmaps. Babel works around this by creating a separate WeakMap for each field. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Private-Named Instance Fields
This PR implements the tc39 class fields proposal for TypeScript. It includes:
PR merge checklist
Example:
ts
js
This PR lead to the following related work by the team:
Babel issues reported:
V8 issues reported:
Related TS PRs:
This PR includes work by the following engineers: