Repository navigation
Design Meeting Notes, 11/17/2017 #20117
Copy link
Copy link
Closed
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Description
Strict property initialization checks in classes
- PR: Strict property initialization checks in classes #20075
- Every non-initialized property that does not have undefined in it type will be an error if not definitely assigned in the constructor, (meaning all code paths needs to have an initializer).
abstractproperties should be excludedT extends number | underfedis an error, since we do not know if it is one or the other at instantiation time- Enabled under new strict mode flag
--strictPropertyInitialization - Issues:
- names in quotes are not checked:
class B { a: number; // Error "b" : number; // No error }
- we do not do control flow analysis on
x["foo"]currently, we only do it onx.foo. - we could do that if we address Discriminant property type guard not applied with bracket notation #10530
- Nathan Shively-Sanders (@sandersn) did have a PR to address that, and the problem was perf
- also applies to symbols, or any computed property names
- we do not do control flow analysis on
- Do we need a new explicit modifier?
- How do you opt-out for a specific class
// @ts-ignore- seems dangerous to rely on this, you change the type, and now no errors
- or put it in
"b"- too cute..
- this looks like a bug that we should fix in the future
- new modifier:
- kotlin uses
lateinit, swift haslazy? - bang operator:
a!: number - extend it to locals as well
- not allowed on
const - should be an error to have it on something with an initializer
readonlyis ok to have!to allow for external- does not amke sense for
statics - Brian Terlson (@bterlson) is not happy about
!- but we have it in expression positions already
- not allowed on
- kotlin uses
- How do you opt-out for a specific class
- method calls in the constructor
- this guards against observing un-initialized properties outside the class, but not inside the class
- this allows for for init patterns
- it is also easy to get around this using aliases, or even through super access
- Daniel Rosenwasser (@DanielRosenwasser), all angular projects, mobx, etc.. that use decorators to initialize properties will have to opt-out of these features
- you can bang these properties
- it is possible that in the future to allow decorators to change the type of the declaration, and in that world we would not need to bang
- this is opt-in at the end of the day, and you can keep the world you are used to
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Activity
DanielRosenwasser commentedon Nov 17, 2017
My notes:
Strict Property Initialization Checks in Classes
| undefinedto a declaration, do we still do initialization checking within the constructor?undefinedas a constraint?undefined@decoratedproperty declarations?!after the property name.lateinit? Some sort of modifier to assure the compiler it doesn't need to write any checks.a!: number = 10?!, it reflects expression level non-null assertion logic.const.readonly.tinganho commentedon Nov 19, 2017
This sounds funny.
Nearly all of our classes are handled by some sort of IoC container.
Sometimes(99% in my case) classes, are just definitions and construction is "inversioned". The IoC container takes care of construction of the class. I don't want the compiler to complain in these type of classes. And providing a flag to opt-out or banging all the properties doesn't sound optimal either.
One suggestion, I can think of, at least for the decorator use case, is to have a modifier on the "definition" class decorator.
In the below example you can see a
constructtargetmodifier:And applying it on entity definitions will not cause any errors:
Example from TypeOrm https://github.com/typeorm/typeorm.
DanielRosenwasser commentedon Nov 19, 2017
We discussed decorators that can declare how they modify the target, so it's an option, but others in the meeting felt like doing so shouldn't be a blocker for the flag as it exists today.
mihailik commentedon Nov 22, 2017
Should this flag hold off from graduating to
--strictset?If it needs to be disabled for a all/large subset of angular code out there, perception of TS as a stable platform might be affected.
mhegazy commentedon Nov 22, 2017
we have the infrastructure in place to opt-out of specific
--strictflags as needed.--strict --strictPropertyInitialization false.--strictby definition means you are accepting breaking changes between releases. if this is not your intention, then do not use--strictand instead spell out the flags you are interested in, e.g.--noImplictAny --strictNullchecksmihailik commentedon Nov 22, 2017
Hope you're right — regardless of technical reasoning you still want gain > pain to proceed
tinganho commentedon Nov 22, 2017
I think many people would rather bang all properties than setting the flag, to just keep the default "strict" mode. But I hope the TS team can hold this off a bit, certainly a contentious topic. As of right now, it feels like a choice between pest and colera.