In JS, there's no way to assert that a property is definitely assigned
See original GitHub issueKeywords: JSDoc, Salsa, JavaScript, definite, initialization, initialized, assigned, assertion
class ElementWrapper {
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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Property '...' has no initializer and is not definitely assigned in ...
Just go to tsconfig.json and set "compilerOptions": { "strictPropertyInitialization": false, ... } to get rid of the compilation error.
Read more >Property has no initializer and is not definitely assigned
Property has no initializer and is not definitely assigned. A quick fix for the most frequent error faced by Angular Developers.
Read more >Documentation - TypeScript 2.0
The compiler checks that variables are definitely assigned by performing control flow based type analysis. See later for further details on this topic....
Read more >Strict Property Initialization in TypeScript - Marius Schulz
The reason for the runtime error is that the username property holds the value undefined because there's no assignment to that property.
Read more >The 10 Most Common JavaScript Issues Developers Face
Memory leaks are almost inevitable JavaScript issues if you're not consciously coding to avoid them. There are numerous ways for them to occur, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DanielRosenwasser had an idea of an
/** @delayinitialized */
on the declaration…We can also add a tag on
init
like/** @initializer */
to treat it like a constructor for purposes of definite assignment detection.The only way I can define is in
Subclass.prototype
after 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