Skip to content

noPropertyAccessFromIndexSignature should not throw errors when property access is a normal identifier (TS4111) #55243

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

Closed
NullVoxPopuli opened this issue Aug 2, 2023 · 3 comments

Comments

@NullVoxPopuli
Copy link

Bug Report

🔎 Search Terms

  • noPropertyAccessFromIndexSignature

🕗 Version & Regression Information

  • version: 5.1.6

problem noticed when noPropertyAccessFromIndexSignature started getting used across codebases

Playgrounds:
5.1.6
nightly

Please keep and fill in the line that best applies:
-->

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything_
  • I was unable to test this on prior versions because _______

💻 Code

type Data = Record<string, string>;

let x: Data = {};

x['foo bar baz'] = '5';

x.foo = '4';
// ^ error 
//   Property 'foo' comes from an index signature, so it must be accessed with ['foo'].(4111)

x.foo;
// ^ error 
//   Property 'foo' comes from an index signature, so it must be accessed with ['foo'].(4111)

🙁 Actual behavior

the noPropertyAccessFromIndexSignature setting is too aggressive and isn't helpful for objects which use identifier keys

🙂 Expected behavior

I almost want there to be a way to specify that keys can be valid identifiers. It seems that noPropertyAccessFromIndexSignature is designed to protect against/for/adhere-to-correctness the fact that "string" (as used in the Key in Record, above) means, literally, "any string", which could include non-identifiers, such as foo bar baz, or foo.bar -- these can be valid keys on an object, but we don't have a way to express that we can manage different kinds of keys.

Sure, an object, by default, is "anything goes" with its keys, but the type system has no way to narrow a key to an identifier.

If I could somehow say:

type Data = Record<Identifier, string>;

then my problem would be solved, and my example could would provide (non) errors like so:

let x: Data = {};

x['foo bar baz'] = '5';
// ^ error
//   Property 'foo bar baz' does not match type Identifier. ( error code )

x.foo = '4';
// no error

x.foo;
// no error
@RyanCavanaugh
Copy link
Member

the noPropertyAccessFromIndexSignature setting is too aggressive and isn't helpful for objects which use identifier keys

I'm really confused about what you're asking for. It's already the case that the only time you can write x.foo is when foo is a legal identifier. If you don't like the behavior, turn off noPropertyAccessFromIndexSignature, this is literally only behavior it controls.

It seems like you're asking for a completely different feature entirely, not describing a defect in NPAFIS

@NullVoxPopuli
Copy link
Author

that is correct, yes

@RyanCavanaugh
Copy link
Member

OK, please use the "Feature request" template on a new issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants