ES private field check by acutmore · Pull Request #44648 · microsoft/TypeScript (original) (raw)
Ergonomic brand checks for Private Fields
This PR implements the TC39 Stage-4 Ergonomic brand checks for Private Fields proposal
Fixes #42574
Remaining Work
- confirm (and release) changes to
tslib
Add new private class element helper: __classPrivateFieldIn tslib#157
Type Checking
Checking the presence of a private field provides a strong type narrowing hint.
class C { #field = 1;
static getFieldOrUndefined(value: object) { if (#field in value) { // 'value' is narrowed from 'object' to 'C' return value.#field; } return undefined; } }
Unsoundness
Whilst the runtime check is accurate, this static check is not fool-proof. The super constructor return pattern can add a private field to an object that is not an instance of the class (different prototype). This PR opts to narrow the type without checking for this case under the assumption that this pattern is uncommon and discoverable by looking at the constructor of the super class.
Example
class Base { constructor(o: object) { return o; } }
class Sub extends Base { #field = 1;
constructor(o: object) {
super(o);
}
method() {}
static run(o: object) {
if (#field in o) {
// 'o' is now of type 'Sub' but...
o.method(); // Throws 'o.method is not a function'
}
}
}
const o = {}; new Sub(o); Sub.run(o);
Downlevel
Builds on top of the existing downlevel support for ES private class elements. If the private field is an instance field then the coresponding WeakMap is consulted. Private methods and accessors consult the WeakSet. Private static fields/methods/accessors do a direct equality check with the class constructor.
Example transform
TypeScript
class Foo { #field = 1; #method() {} static #staticField = 2; static #staticMethod() {}
check(v: any) {
#field in v;
#method in v;
#staticField in v;
#staticMethod in v;
}
}
function syntaxError(v: Foo) { return #field in v; }
JavaScript - ES2020 emit
var __classPrivateFieldIn = (this && this.__classPrivateFieldIn) || function(receiver, state) { if (receiver === null || (typeof receiver !== "object" && typeof receiver !== "function")) throw new TypeError("Cannot use 'in' operator on non-object"); return typeof state === "function" ? receiver === state : state.has(receiver); }; var _Foo_instances, _a, _Foo_field, _Foo_method, _Foo_staticField, _Foo_staticMethod, _Bar_field; class Foo { constructor() { _Foo_instances.add(this); _Foo_field.set(this, 1); } check(v) { __classPrivateFieldIn(v, _Foo_field); __classPrivateFieldIn(v, _Foo_instances); __classPrivateFieldIn(v, _a); __classPrivateFieldIn(v, _a); } } _a = Foo, _Foo_field = new WeakMap(), _Foo_instances = new WeakSet(), _Foo_method = function _Foo_method() { }, _Foo_staticMethod = function _Foo_staticMethod() { }; _Foo_staticField = { value: 2 };
function syntaxError(v) { return in v; }
References
- Spec
- TypeScript
- Engines
- V8 9.1 blog
- Chrome shipped in v91
- FireFox shipped in v90
- Safari merged
- Babel
Credits
This PR includes contributions from the following Bloomberg engineers:
- Titian Cernicova-Dragomir @dragomirtitian : support, advice, ideas, PR review
- Kubilay Kahveci @mkubilayk : PR review
- Rob Palmer @robpalme : support, advice