-
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
Compiler should allow assignment of new property in empty object #11839
Comments
Would this change stop the compiler from identifying typos? e.g. what if What would the rules be for when a new property can be added, and when the 'property X does not exist' error would still be raised? |
|
We infer an implicit let x;
let y = null;
let z = undefined; With #11263 we introduced control flow analysis for such implicit let x;
x = {};
x.foo = 42; // Error, {} has no 'foo' property The code above used to compile because Some older code bases that compile without Alternatively, we could revert back to the old behavior when I'm curious what people think? I'm personally on the fence here. |
One thing to note is for JS files the new behavior is more desirable. so either ways, we should keep it working in JS files. |
As someone who generally avoids |
@aluanhaddad I'm curious why you avoid |
@ahejlsberg It's something I'm conflicted about. The main reason why I avoid it is that I find that, while evolving a piece of code, some types are naturally in flux. Since Unfortunately I find that the code is rarely revisited with this intent and so the That left over I am open to being persuaded otherwise 😃. |
Here is something I see fairly often, basically verbatim. First Pass export let fields: any; Second Pass export let fields: any = {
name: { match: /[a-zA-Z]+/g, value: '', valid: false },
age: { match: /[0-9]+/, value: '', valid: false }
}; The second pass makes me very sad. EDIT array -> object |
Let's talk about this at the design meeting. @ahejlsberg and I think leaving it as-is (keeping the new behaviour everywhere) is worthwhile. It is a break, but only for remarkably un-Typescript-like code, and it is easy to work around the break. Besides, 100% of the people we surveyed want this feature even with |
//cc: @DanielRosenwasser |
@mhegazy Would it be possible to have this under a separate flag? I realize that it is important to be conservative in adding new flags, but I would really like to take advantage of this future without enabling Additionally I think many users who do not currently use this option could actually stand to benefit most from this feature. I believe that the fact that it will still be enabled for JavaScript files underscores my point. |
We could revisit this in the future, assuming we have support for the different object construction pattern and a consistent error reporting experience. |
@mhegazy thanks I appreciate it. Should I open an issue for it? |
Would you mind defining this? I want to know if the code that I am writing is "un-Typescript-like" 😃 |
Basically, writing code that makes it harder for the compiler to get the types right instead of easier. A simple example is writing ES5-style classes instead of ES6 From the bug, I would expect to see var range;
range = {}
range.newProp = 10; // error Written as var range = { newProp: 10 }; It's more succinct and easier for the compiler to analyse. |
@sandersn Thank you for answering! I definitely prefer declarative forms such as those you mention. They are easier to reason about for humans as well for compilers. I think what you are saying is that clean, declarative JavaScript is idiomatic TypeScript. That definitely aligns with my use of the language. |
TypeScript Version: built from master
Code
Expected behavior:
No error here
range.newProp = 10;
orobj.propy = 20;
Actual behavior:
Error TS2339 Property 'newProp' does not exist .... or Property 'propy' does not exist ...
The text was updated successfully, but these errors were encountered: