Skip to content

noPropertyAccessFromIndexSignature does not affect object literalsΒ #46435

Open
@concavelenz

Description

@concavelenz

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.

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