Compilation option: stricterPropertyInitialization
#60906
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
π Search Terms
Keywords: "stricter property initiazliation"
β Viability Checklist
β Suggestion
So, TypeScript offers the
strictPropertyInitialization
compiler option, which is helpful, but it has this behavior:This is great except for one thing: performance. Uninitialized properties in the class (specifically:
address
) will, at least in V8, potentially switch the class to dictionary mode vs struct mode when the property eventually gets assigned, leading to slower read/writes. This was confirmed by a V8 developer in this StackOverflow thread.So the suggestion is that
stricterPropertyInitialization
setsstrictPropertyInitialization
totrue
AND throws an error if the author has added| undefined
to the type to implicity define the type. In other words, "strict property initialization" would mean what it says: strict property initialization. If it is not initialized, it is an error. In the above example, from the tsconfig docs, theaddress
is not initialized. It's too late to change that as default behavior, hence the additional flag.Note: maybe
stricterPropertyInitialization
is too awkward? Something likedisallowImplicitUndefinedClassFields
? π€·ββπ Motivating Example
Most devs probably won't need to worry about the performance implications of internal dictionary vs. struct. In my case, in the library I'm maintaining / working on, every millisecond / fraction of a millisecond counts, so I'm trying to determine the fastest path in every scenario. I expected
strictPropertyInitialization
to mean what it says to prevent properties from not being initialized and avoid any accidental performance pitfalls from non-initialization, but it doesn't. π€·ββMeaning, in the above example:
So, most simply,
stricterPropertyInitialization
would throw a compilation error if| undefined
is added to a property and the property is not explicitly initialized with a field initializer or in the constructor.If the code author wishes to actually initialize the field, to undefined, it must be something like:
π» Use Cases
What do you want to use this for?
A high-performance TS/JS library
What shortcomings exist with current approaches?
There are no workarounds as far as I know. You simply have to let all devs on your team know to not write this and hope people catch it.
What workarounds are you using in the meantime?
Vigilance
The text was updated successfully, but these errors were encountered: