-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uninitialized class properties with type undefined are allowed, breaking type expectations for class instances. #55195
Comments
Duplicate of #28823 / #12437. As per #12437 the suggested solution is to use Also be aware that the |
That is working as intended. As per #20075:
It's not a bug. You're free to disagree of course, but the position of the team is quite clear on this. New features have always been "hidden" behind flags to keep backwards compatibility, even starting with something like Be also aware that TypeScript intentionally does not have a sound type system. There is plenty of unsound type behaviour that is intentional. https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals#non-goals
|
I would call that a bug - the type system is giving it a type that it doesn't conform to. The type checker is then telling us things that are not true, based on that typing. Thanks for the link to #20075, I see that the implementation was by design - but there was no acknowledgement of the issue, so I'm not sure we can say that the bug is by design from that thread.
I would hope that the meaning of this, is that developers are allowed to do things like use Without useDefineForClassFields this class: class C { // new C will produce an empty object, because a is not initialized
a: undefined
b: boolean | undefined
} produces objects of type I don't know that the "back compatibility" argument applies when we are talking about TS mistyping objects. I would think bugs should be fixed, in order to alert users to possible bugs in their app, caused by TS mis-typing. |
Here's the acknowledgement: #52259
No, it does not mean that. It means there are several aspects of the type system that are intentional wrong. Here's another example that is intentional (and please don't open an issue about this..): class Foo { a: string = 'abc' }
const f: Foo = { a: 'abc '}
if (f instanceof Foo) {
console.log('true');
} else {
// type is never here, but this branch will execute.
console.log('false')
} Related: #55113 (comment) |
But it still doesn't acknowledge that the objects are mis-typed : )
But I think again this is a productivity shortcut. Do you think that the behaviour in this ticket is a productivity shortcut? What is the productivity benefit that justifies type incorrectness here? |
It acknowledges that this is working as intended. Do you want for every issue a wording that matches your preconception? Anyway, I'm out, this is pointless. |
By the way do you have a link to where this was discussed? Interested to read about the reasoning. |
Re: Intentional unsoundness - #9825, and in particular #9825 (comment) |
Thanks. Should probably add the 3rd factor to the design doc though. |
I don't have anything to add to the above comments. |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
class properties with type undefined properties produce objects that don't match inferred type
π Version & Regression Information
Present between 3.3 and 5.2.0 nightly.
β― Playground Link
Playground
π» Code
π Actual behavior
new C
produces an empty objectc
c
has a propertya
, based on the class definitiono: O
is allowedf
, TS identifies that theelse
branch doesn't occurf(o)
orf(c)
results in theelse
branch runningOutput
π Expected behavior
-strict mode was on
a
is not initialized, and throw an errorNote: If useDefineForClassFields is true, which occures by default for target >= ES2022, JS is emitted which initializes the property, and there is no problem. Bug exists only for cases of useDefineForClassFields: false (includes the default TS config)
The text was updated successfully, but these errors were encountered: