-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
noPropertyAccessFromIndexSignature
does not affect object literals
#46435
Comments
Can you expound on each of these motivations? |
I believe for logic errors, I believe this has the same motivations as Closure Compiler as you may know is used heavily at Google. |
Similarly, object destructuring assignment is similar to property access, I would expect that destructuring an index signature would also give an error with noPropertyAccessFromIndexSignature: type Dictionary = Record<string, string>;
let test: Dictionary = {
// we want this to be caught by noPropertyAccessFromIndexSignature, but it isn't.
abc: 'hi',
def: 'hello',
};
// This is equivalent to getting test.abc, so should be an error
const {abc} = test;
// This is preferred
const {'abc': abc1} = test; The Closure Compiler uses the quoting for all of these syntaxes to determine if the property name will be renamed. |
@andrewbranch Where would I find the justification for |
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#--nopropertyaccessfromindexsignature. Following the link to the PR should turn up more discussion. |
IIRC, @DanielRosenwasser is pretty enthusiastic about this flag. Personally, I think it belongs in a linter. |
Bug Report
π Search Terms
noPropertyAccessFromIndexSignature
π Version & Regression Information
4.2+
β― Playground Link
https://www.typescriptlang.org/play?noPropertyAccessFromIndexSignature=true#code/FAGwpgLgBBYM4QFxQN4G0CWA7AJmAHoggE7YDmAukRKVmQL5QC8qwU7UA9J1AO5h8AhlmgQAFhjgwA9lABGAgMaCArmTHQ5ATyhZpABWLSADmGIQtAQUWL4cAGJGAtgElcBAMoYyWQRBXEYAA08irQGOFwWADkEAB0bByCcorI0RLRQYnseABmaWJgICDSmcD0ANzAwIrSWHDS4HElZAAUsAhxyYoAlFW19Y1gzdJtHfF5fTV1DU0t7fAQaNHd0RRTA7PD8+PLeWt9QA
π» Code
π Actual behavior
Unquoted string keys are allowed when assigned to types with index signatures.
π Expected behavior
Some mechanism for restricting the definitions of object literals so that only named properties are allowed to be provided without quotes (
{['somekey']: value}
or{'somekey':value}
. This would both prevent logic errors and improve compatibility with Closure Compiler's ADVANCED optimizations.The text was updated successfully, but these errors were encountered: