-
Notifications
You must be signed in to change notification settings - Fork 12.8k
In JS, there's no way to assert that a property is definitely assigned #23217
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
Comments
@DanielRosenwasser had an idea of an We can also add a tag on |
Wouldn't simply following the constructor call chain be simpler? |
Since someone mentioned following the constructor call chain: |
Thanks for the heads-up. I'm always surprised when I remember this isn't the case. |
The only way I can define is in class Superclass extends HTMLElement {
static {
console.log(1, 'Super Static Init')
}
static superStaticField = console.log(2, 'Super Static Field')
static observedAttributes = ['foo'];
constructor() {
console.log(8, 'Superclass constructor: pre-super()');
super();
this.superConstructorVar = console.log(10, 'Superclass constructor: post-super()');
}
superInstanceField = console.log(9, 'Superclass Instance Field');
}
Superclass.prototype.superInstanceProp = console.log(3, 'Super post-declaration' );
class Subclass extends Superclass {
static {
console.log(4, 'Sub Static Init');
this.prototype.a = 1; // ts-error
Subclass.prototype.b = 2 // ts-error
}
static subStaticField = console.log(5, 'Sub Static Field');
static get observedAttributes() {
console.log(6.5, 'HTMLElement reads observedAttributes from subclass');
return ['foo'];
}
attributeChangedCallback() {
console.log(13, 'Attributes observed');
}
constructor() {
console.log(7, 'Subclass constructor: pre-super()')
super();
this.subConstructorVar = console.log(12, 'Subclass constructor: post-super()');
}
subclassInstanceField = console.log(11, 'Subclass Instance Field');
}
Subclass.prototype.subInstanceProp = console.log(6, 'Sub post-declaration');
customElements.define('x-sub', Subclass);
console.log('Element added to registry');
const el = document.createElement('x-sub');
console.log('Element created');
el.setAttribute('foo', 'bar');
console.log('Attribute added'); This yields: 1 'Super Static Init'
2 'Super Static Field'
3 'Super post-declaration'
4 'Sub Static Init'
5 'Sub Static Field'
6 'Sub post-declaration'
6.5 'HTMLElement reads observedAttributes from subclass'
Element added to registry
7 'Subclass constructor: pre-super()'
8 'Superclass constructor: pre-super()'
9 'Superclass Instance Field'
10 'Superclass constructor: post-super()'
11 'Subclass Instance Field'
12 'Subclass constructor: post-super()'
Element created
13 'Attributes observed'
Attribute added I have to use prototype because _a = Subclass;
(() => {
console.log(4, 'Sub Static Init');
_a.prototype.a = 1;
Subclass.prototype.b = 2;
})(); I'm not sure why the IIFE is needed, but if it can reworked, I think it would be fine. |
Following the program flow is not always possible. class R {
accept;
constructor() {
new Promise(accept => this.accept = accept);
}
} |
It is possible to follow all of the constructor's program flow in that example. |
@beauxq you're wrong on two points. |
When I say it's impossible to know if the Promise constructor runs the executor before returning I mean from the type system's point of view. |
I see. |
Because the type system has no way of expressing that. |
That's just an assertion of what I'm saying I don't see. |
Keywords: JSDoc, Salsa, JavaScript, definite, initialization, initialized, assigned, assertion
We need a way to convince TypeScript in
strictNullChecks
thatthis.element
is initialized ingetElementStyle
. Basically a definite initialization assertion.Related is #23405 which tracks non-null assertions on the expression level.
The text was updated successfully, but these errors were encountered: