Skip to content

Inconsistency between element access and object destruction #59571

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

Open
kermanx opened this issue Aug 9, 2024 · 1 comment
Open

Inconsistency between element access and object destruction #59571

kermanx opened this issue Aug 9, 2024 · 1 comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@kermanx
Copy link

kermanx commented Aug 9, 2024

πŸ”Ž Search Terms

destruct object

πŸ•— Version & Regression Information

Before v4.4.4, index expression in element access was also not inferred as const. So this issue starts from v4.4.4. Reproducible in Nightly build.

⏯ Playground Link

https://www.typescriptlang.org/play/#code/DYUwLgBAdhC8EEYIEMDOEDGB7KqwChtdIs4IBvFBALggCIE6V0i8IBffQnNgDzKwBtAAbIAJOSjthAXXwB6eRGUQAegH5uxChBHjJ0mbQCeHAfiA

πŸ’» Code

let n = 1 as const
const o = { a1: "abcd" }

const x = o[`a${n}`]
//    ^? : string
const { [`a${n}`]: y } = o
//           ^? Errors: Type '{ a1: string; }' has no matching index signature for type 'string'.(2537)

πŸ™ Actual behavior

`a${n}` in object destruction not inferred as const ("a1")

πŸ™‚ Expected behavior

`a${n}` in object destruction should be inferred as "a1", just like the one in element access.

Additional information about the issue

A possible workaround is to add as const after `a${n}`. I think the main problem is the inconsistency between element access and object destruction, which is confusing.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Aug 9, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 9, 2024
@sparecycles
Copy link
Contributor

Another destructuring inconsistency, (I'm not sure if the fix would be related, but cross-linking just in case).

#59581

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

3 participants