-
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
Objects with string indexing should be compiled with actual string indicies #15206
Comments
What problem is this causing? Traditionally strings as keys of object literals are only quoted when the string contains something that would not be a valid variable name in JavaScript (e.g. 'a key like this'). |
The problem is that without using actual strings minifiers are prone to rename the symbol. This becomes a problem when receiving json's via http for example where the server constructs a message with the non renamed symbol |
Most minifiers do not mangle object literal keys unless they are instructed it is safe to do so because of exactly the problem you mention. The developer has every right to specify object literals as strings or not, if that meets their particular use case. Having TypeScript universally enforce one way or the other would likely not be a good idea. |
I would agree with that. Okay let's take a step back and imagine a more
specific developer scenario. Imagine a team trying to enforce that a
specific object only has its properties accessed using bracket notation.
Prior to typescript 2.2 this was possible. Now it is not. So typescript 2.2
has introduced a regression of language features for developers in this
category. Perhaps this regression warrants a flag of some sort?
…On Sun, Apr 16, 2017, 1:04 AM Kitson Kelly ***@***.***> wrote:
Most minifiers do not mangle object literal keys unless they are
instructed it is safe to do so because of exactly the problem you mention.
The developer has every right to specify object literals as strings or
not, if that meets their particular use case. Having TypeScript universally
enforce one way or the other would likely not be a good idea.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#15206 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHteO2r2aXGR23nToVo13Vnm6J1Iu3tDks5rwb1ngaJpZM4M-aTr>
.
|
Again, what problem would this be causing? If anything, it would appear to be a stylistic choice then, which is the domain of a linter. |
I'm using Angular 2 with the closure compiler. Angular's form's has an implicit requirement that FormGroups values be access with string indicies so that the closure compiler does not minify them. |
By the way, AFAIU this is not limited to Closure compiler, you'll have the same problem when using Uglify in advanced mode. |
See also #14267 files by @alexeagle |
For any code that is "normal" JS, we're going to emit the same code as what you wrote. The onus is on the developer to have the right rules / process around whether or not the code they're writing is correct for downstream heuristic-based tools. |
Ryan: previously, we were able to use TypeScript to express the
process/rules around quoting for optimization – you'd simply get errors in
your IDE if you get the `['quoting']` vs `.dotting` wrong due to index
accessor/no index accessor.
We can no longer express this in TypeScript, which means people run into
errors, which means we'll apparently have to build our own toolchain to do
this (which is effort, and likely won't be as nicely integrated into e.g.
the IDE experience).
That's quite a loss in usability of TS for us.
|
One person's improvement is another's regression. It appears that people were depending on a side-effect versus a feature. |
It should be very simple to write a TSLint rule to detect "used dotting to access string index signature" (or vice versa) mismatches, assuming one doesn't exist already. |
This issue seems to have been already addressed or is unactionable at the moment. I am auto-closing it for now. |
TypeScript 2.3 allows users to use `dotted.access` for types that (only) have a string index type declared, e.g. `{[k: string]: ...}`. This breaks renaming in tools such as Closure Compiler (which tsickle targets), as some locations might access the property with quotes, some without. This is an incomplete fix: TypeScript allows assigning values with properties of the matching type into types with index signatures. Users can then access the original value with dots, but the aliased value with quotes, which breaks, too. This is probably not fixable without a global aliasing analysis of the program. See also: microsoft/TypeScript#14267 microsoft/TypeScript#15206
TypeScript 2.3 allows users to use `dotted.access` for types that (only) have a string index type declared, e.g. `{[k: string]: ...}`. This breaks renaming in tools such as Closure Compiler (which tsickle targets), as some locations might access the property with quotes, some without. This is an incomplete fix: TypeScript allows assigning values with properties of the matching type into types with index signatures. Users can then access the original value with dots, but the aliased value with quotes, which breaks, too. This is probably not fixable without a global aliasing analysis of the program. See also: microsoft/TypeScript#14267 microsoft/TypeScript#15206
TypeScript 2.3 allows users to use `dotted.access` for types that (only) have a string index type declared, e.g. `{[k: string]: ...}`. This breaks renaming in tools such as Closure Compiler (which tsickle targets), as some locations might access the property with quotes, some without. This is an incomplete fix: TypeScript allows assigning values with properties of the matching type into types with index signatures. Users can then access the original value with dots, but the aliased value with quotes, which breaks, too. This is probably not fixable without a global aliasing analysis of the program. See also: microsoft/TypeScript#14267 microsoft/TypeScript#15206
TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)
Code
Expected behavior:
The compiler should generate an object with string properties (notice how a is in quotes below). This behavior allows for proper minification of the generated code since minifiers such as the closure compiler will not rename strings.
Actual behavior:
Unfortunately, even thought the type of the object specified that all boject properties are strings, the generated code does not wrap the property name in quotes.
The text was updated successfully, but these errors were encountered: