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
Breaking ChangeWould introduce errors in existing codeES NextNew featurers for ECMAScript (a.k.a. ESNext)SuggestionAn idea for TypeScriptWaiting for TC39Unactionable until TC39 reaches some conclusion
Actual behavior:
class PartialPolicySearchIten { }
The problem comes when using Reflection it can't find the properties. I would expected that the properties exists as i'm redefining them. Otherwise their very little point is declaring properties of a class.
Reacted by Nicolas HENRY, Nicolò Ribaudo, sepehr, hasezoey, HL, Eyal Lapid, Ivan Pantic, Preom Rahman, Elad Ossadon, anth-git and 2 more
While this is the original/current behavior, the current ES proposal seems to actually imply that a property will indeed be defined & set to undefined even without an initializer.
(Public) class fields are enabled by default in Chrome 72 with the initializer semantics outlined above. This part of the spec is extremely unlikely to change. Current topics of debate are just how private fields should work AFAICT.
Can we please revisit and get a decision here? While the Babel transform is not officially supported, they would want to wait for a decision by the TypeScript team before implementing any fixes: babel/babel#9105
The major arguments for keeping the current semantics to me are:
no breakage
"re-typing" a class field in a derived class is easier.\
In favor of switching:
spec compliancy
less cognitive load for JS developers migrating to TS
Any input would be much appreciated ☺️❤️
Reacted by Nicolò Ribaudo, Christian, jstaro, Chris Krycho, BjarkeNL, Brian M Hunt, Nicolas HENRY, hasezoey and Eyal Lapid
Tomer Kruvi (@kutomer) I think what Nicolò Ribaudo (@nicolo-ribaudo) wanted to express is that the declare keyword could be used as new syntax, i.e. it does not work yet and would be a breaking change to TypeScript. 🙂
Also worth noting here that moving to spec compliance is a loud breaking change, because the existing types end up being wrong with strictNullChecks: true:
classFoo{publicx: string;// initialized to undefined}
☝️ would be wrong; the type would need to be:
classFoo{publicx?: string;// initialized to undefined}
This will be a non-trivial breaking change to a lot of codebases. (It intersects with definite strictPropertyInitialization a bit as well.)
Jordan Harband (@ljharb) I ask again for the use case.
Also, if you do not initialise and optional property, why do you expect it to be initialised with undefined? If you do not initialise an optional property, the expected behaviour is it's not there, uninitialised shouldn't be initialised, and that's exactly what happens.
Also, the OP example is horrible.
If you define a property as non-optional number, it should be nothing else, but a number. No part of the code says it can be undefined.
Anything using object spread or Object.assign will care about it being a present, own property, even if set to undefined. undefined is not simply "not defined", in JS, there exists "absent", "present but set to undefined", and "present but set to non-undefined".
Then initialise your properties explicitly. This is a breaking change, it breaks the logic behind explicit property initialisation and, once again, I do not see any use case where it should matter.
In OOP, you shouldn't really ask for properties of a class instance, because it breaks its encapsulation.
In FP, you wouldn't even use a class for this, and therefore you would have to initialise the property on your own.
This change is nothing but an attempt to encourage a bad programming style.
I also do not see, why you would want to run a class instance against Object.assign or object spread. Mind to elaborate?
Give me a valid example of why the lack of implicit property initialisation in a class instance is an issue.
Igor Szymański (@IgorSzymanski) this continual moving of the goalposts isn't indicative of a good-faith conversation. If you don't think it's important to you, that's fine, but no one is required to justify spec compliance as a valid goal
Reacted by Jordan Harband, Alex Kritchevsky, anth-git, Jaron Summers and Micka190
The reason I initially came across this issue, and wrote my comment above, is that I hit a case where an error was caused by this surprising logic. Unfortunately I cannot remember the exact example, but any similar example would be equivalent: if one writes
class Foo {
public x?: string;
}
and doesn't initialize the property, there should be some sort of warning.
Breaking ChangeWould introduce errors in existing codeES NextNew featurers for ECMAScript (a.k.a. ESNext)SuggestionAn idea for TypeScriptWaiting for TC39Unactionable until TC39 reaches some conclusion
Activity
Jameskmonger commentedon Nov 22, 2016
You need to explicitly initialise them as undefined to have the effect you want.
Input
Output
Therefore I think that this is not an issue but is intended behaviour.
DanielRosenwasser commentedon Nov 22, 2016
While this is the original/current behavior, the current ES proposal seems to actually imply that a property will indeed be defined & set to
undefinedeven without an initializer.https://tc39.github.io/proposal-class-public-fields/#initialize-public-static-fields
RyanCavanaugh commentedon Jan 10, 2018
The proposal is now Stage 3 with this behavior. We need to discuss whether or not to take a break for this
buschtoens commentedon Dec 3, 2018
(Public) class fields are enabled by default in Chrome 72 with the initializer semantics outlined above. This part of the spec is extremely unlikely to change. Current topics of debate are just how private fields should work AFAICT.
Can we please revisit and get a decision here? While the Babel transform is not officially supported, they would want to wait for a decision by the TypeScript team before implementing any fixes: babel/babel#9105
The major arguments for keeping the current semantics to me are:
In favor of switching:
Any input would be much appreciated☺️ ❤️
nicolo-ribaudo commentedon Dec 4, 2018
For type-only information something like this could work:
kutomer commentedon Dec 4, 2018
Nicolò Ribaudo (@nicolo-ribaudo) not sure how it's possible, tried it on ts playground and got
declare' modifier cannot appear on a class element.buschtoens commentedon Dec 4, 2018
Tomer Kruvi (@kutomer) I think what Nicolò Ribaudo (@nicolo-ribaudo) wanted to express is that the
declarekeyword could be used as new syntax, i.e. it does not work yet and would be a breaking change to TypeScript. 🙂nicolo-ribaudo commentedon Dec 4, 2018
Yeah exactly
chriskrycho commentedon Dec 26, 2018
Also worth noting here that moving to spec compliance is a loud breaking change, because the existing types end up being wrong with
strictNullChecks: true:☝️ would be wrong; the type would need to be:
This will be a non-trivial breaking change to a lot of codebases. (It intersects with definite
strictPropertyInitializationa bit as well.)8 remaining items
ljharb commentedon Jun 8, 2020
Igor Szymański (@IgorSzymanski) the difference, however, is observable, and it's important for it to be correct.
IgorSzymanski commentedon Jun 10, 2020
Jordan Harband (@ljharb) I ask again for the use case.
Also, if you do not initialise and optional property, why do you expect it to be initialised with undefined? If you do not initialise an optional property, the expected behaviour is it's not there, uninitialised shouldn't be initialised, and that's exactly what happens.
Also, the OP example is horrible.
If you define a property as non-optional
number, it should be nothing else, but anumber. No part of the code says it can beundefined.ljharb commentedon Jun 10, 2020
Anything using object spread or
Object.assignwill care about it being a present, own property, even if set toundefined.undefinedis not simply "not defined", in JS, there exists "absent", "present but set to undefined", and "present but set to non-undefined".IgorSzymanski commentedon Jun 10, 2020
Then initialise your properties explicitly. This is a breaking change, it breaks the logic behind explicit property initialisation and, once again, I do not see any use case where it should matter.
In OOP, you shouldn't really ask for properties of a class instance, because it breaks its encapsulation.
In FP, you wouldn't even use a class for this, and therefore you would have to initialise the property on your own.
This change is nothing but an attempt to encourage a bad programming style.
I also do not see, why you would want to run a class instance against
Object.assignor object spread. Mind to elaborate?Give me a valid example of why the lack of implicit property initialisation in a class instance is an issue.
RyanCavanaugh commentedon Jun 10, 2020
Igor Szymański (@IgorSzymanski) this continual moving of the goalposts isn't indicative of a good-faith conversation. If you don't think it's important to you, that's fine, but no one is required to justify spec compliance as a valid goal
ajakaja commentedon Jun 10, 2020
The reason I initially came across this issue, and wrote my comment above, is that I hit a case where an error was caused by this surprising logic. Unfortunately I cannot remember the exact example, but any similar example would be equivalent: if one writes
and doesn't initialize the property, there should be some sort of warning.
Otherwise
leads to confusion. For instance, sometimes one uses
hasOwnPropertyto implement a typeguard:which fails if
Foois defined as above, even though it might be an actual instance ofFoo.IgnusG commentedon Mar 9, 2021
Any update on the spec compliance?
RyanCavanaugh commentedon Mar 9, 2021
This is now the behavior under
--useDefineForClassFieldsljharb commentedon Mar 9, 2021
Is that also now the default in tsc init?
RyanCavanaugh commentedon Mar 9, 2021
Not yet, but we should probably change that.
ljharb commentedon Mar 9, 2021
Should this be reopened to track that, or would you prefer a separate issue?
RyanCavanaugh commentedon Mar 9, 2021
Separate issue please. Thanks!
ljharb commentedon Mar 9, 2021
Filed #43167.
ExE-Boss commentedon Mar 10, 2021
Looks like there’s already #39311.