Skip to content

noPropertyAccessFromIndexSignature does not affect object literalsΒ #46435

Open

Description

Bug Report

πŸ”Ž Search Terms

noPropertyAccessFromIndexSignature

πŸ•— Version & Regression Information

4.2+

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?noPropertyAccessFromIndexSignature=true#code/FAGwpgLgBBYM4QFxQN4G0CWA7AJmAHoggE7YDmAukRKVmQL5QC8qwU7UA9J1AO5h8AhlmgQAFhjgwA9lABGAgMaCArmTHQ5ATyhZpABWLSADmGIQtAQUWL4cAGJGAtgElcBAMoYyWQRBXEYAA08irQGOFwWADkEAB0bByCcorI0RLRQYnseABmaWJgICDSmcD0ANzAwIrSWHDS4HElZAAUsAhxyYoAlFW19Y1gzdJtHfF5fTV1DU0t7fAQaNHd0RRTA7PD8+PLeWt9QA

πŸ’» Code

let test: {[index:string]:string} = {
    // we want this to be caught by noPropertyAccessFromIndexSignature, but it isn't.
    abc: 'hi',
    def: 'hello',
};

console.log(test.abc); // reported
console.log(test.def); // reported
console.log(test['abc']); // OK
console.log(test['def']);  // OK

πŸ™ 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.

Activity

  1. andrewbranch commented on Oct 19, 2021

    @andrewbranch
    Member

    This would both prevent logic errors and improve compatibility with Closure Compiler's ADVANCED optimizations.

    Can you expound on each of these motivations?

  2. concavelenz commented on Oct 20, 2021

    @concavelenz
    Author

    I believe for logic errors, I believe this has the same motivations as noPropertyAccessFromIndexSignature. For Closure Compiler's ADVANCED optimization mode, it differentiates between rename-able (optimizable) properties based on whether the property is "quoted". Quoted and computed properties are not renamed or eligible for optimization but "." access/unquoted properties are. noPropertyAccessFromIndexSignature is helpful here as it encourage consistent use of "quoting". One of the teams we support is looking for the some degree of protection for object literals assigned to types with index signatures.

    https://developers.google.com/closure/compiler/docs/limitations#implications-of-global-variable,-function,-and-property-renaming:

    Closure Compiler as you may know is used heavily at Google.

  3. RussellSprouts commented on Oct 25, 2021

    @RussellSprouts

    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;

    https://www.typescriptlang.org/play?noPropertyAccessFromIndexSignature=true#code/FAFwngDgpgBAIgSwMYgQewHYEMBOYYC8MASlEmjgCYA8AziDghgOYA0M9jLAfANzDAANlBAwQUegC54yVJlz4iAb2Aw1MAPQaYAd1g6sGUSAAWCWmLQwARrCRYArsxOjr+DGgAKONNBzgAQSQkCVoAMR8AWwBJDEooAA8AZQRmbBAHHCh2awdRBHzaDAByEAA6VXUsayRpYrNi1kq1eIAzOpMoQUE0RuAAX35gLRgAFTMLcxgoAEcHBAA3LGEjSzEJcuqkdloTNAdBShtYQ2mcHxxgcgx6GCUt-sJ1+n5r26VireLpLYBGR6I4heAjeaGEZR6zAAFEDNjUAJSvTC0MFQCFoaGwsptRFXZGo9GYjYAbU+NWKAF1caDwZCYSTim1KYigA

    The Closure Compiler uses the quoting for all of these syntaxes to determine if the property name will be renamed.

  4. concavelenz commented on Oct 28, 2021

    @concavelenz
    Author

    Andrew Branch (@andrewbranch) Where would I find the justification for noPropertyAccessFromIndexSignature?

  5. andrewbranch commented on Oct 28, 2021

    @andrewbranch
    Member
  6. concavelenz commented on Oct 29, 2021

    @concavelenz
    Author

    ic since Wenlu Wang (@Kingwl) contributed #40171, I shouldn't expect the core TypeScript team to take an interest in this, I suppose.

  7. andrewbranch commented on Oct 29, 2021

    @andrewbranch
    Member

    IIRC, Daniel Rosenwasser (@DanielRosenwasser) is pretty enthusiastic about this flag. Personally, I think it belongs in a linter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions