You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In JS, there's no way to assert that a property is definitely assigned #23217
classElementWrapper{constructor(){this.init();}init(){this.element=document.createElement('div');}getElementStyle(){this.element.style// error: element is possibly undefined}}
We need a way to convince TypeScript in strictNullChecks that this.element is initialized in getElementStyle. Basically a definite initialization assertion.
Related is #23405 which tracks non-null assertions on the expression level.
changed the title [-]Definite assignment assertions in JSDoc[/-][+]In JS, there's no way to assert that a property is definitely assigned[/+]on Apr 6, 2018
I have to use prototype because constructor() and instance class fields are too late. (I would like to append to the shadowroot based on class properties). I've resolved to using post-declaration at the end of the file, but defining properties at the end of the class is awkward. I would rather use the Class static initialization blocks, but Typescript will not define the type there. That's because it rewrites that code as:
It is possible to follow all of the constructor's program flow in that example. And accept is not initialized before the end of that constructor. That is not a relevant example.
Doug Hoskisson (@beauxq) you're wrong on two points.
It's not possible to follow the program flow because it's impossible to know if the Promise constructor will run the function before it returns, which it does, making you wrong on the second point: accept is initialized before the end of the constructor.
It's fine to be wrong and it's ok to arrogant. Being arrogant and wrong is just hilarious.
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.
We know, of course, it does.
Activity
mhegazy commentedon Apr 6, 2018
Daniel Rosenwasser (@DanielRosenwasser) had an idea of an
/** @delayinitialized */on the declaration..We can also add a tag on
initlike/** @initializer */to treat it like a constructor for purposes of definite assignment detection.[-]Definite assignment assertions in JSDoc[/-][+]In JS, there's no way to assert that a property is definitely assigned[/+]mqudsi commentedon Apr 26, 2018
Wouldn't simply following the constructor call chain be simpler?
beauxq commentedon Nov 8, 2020
Since someone mentioned following the constructor call chain:
#30462
mqudsi commentedon Nov 10, 2020
Thanks for the heads-up. I'm always surprised when I remember this isn't the case.
clshortfuse commentedon Oct 27, 2022
The only way I can define is in
Subclass.prototypeafter the declaration:This yields:
I have to use prototype because
constructor()and instance class fields are too late. (I would like to append to the shadowroot based on class properties). I've resolved to using post-declaration at the end of the file, but defining properties at the end of the class is awkward. I would rather use theClass static initialization blocks, but Typescript will not define the type there. That's because it rewrites that code as:I'm not sure why the IIFE is needed, but if it can reworked, I think it would be fine.
Playground Link
ilogico commentedon Mar 17, 2023
Following the program flow is not always possible.
Consider this:
beauxq commentedon Mar 17, 2023
It is possible to follow all of the constructor's program flow in that example.
Andacceptis not initialized before the end of that constructor. That is not a relevant example.ilogico commentedon Mar 17, 2023
Doug Hoskisson (@beauxq) you're wrong on two points.
It's not possible to follow the program flow because it's impossible to know if the Promise constructor will run the function before it returns, which it does, making you wrong on the second point: accept is initialized before the end of the constructor.
It's fine to be wrong and it's ok to arrogant. Being arrogant and wrong is just hilarious.
ilogico commentedon Mar 17, 2023
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.
We know, of course, it does.
beauxq commentedon Mar 17, 2023
I see.
I don't see why you think it's impossible to know if the Promise constructor will run the function before it returns.
ilogico commentedon Mar 17, 2023
Because the type system has no way of expressing that.
beauxq commentedon Mar 17, 2023
That's just an assertion of what I'm saying I don't see.