Microsoft/TypeScript
Auf GitHub ansehenInconsistency between element access and object destruction
Open
#59.571 geöffnet am 9. Aug. 2024
Domain: check: Type InferenceHelp WantedPossible Improvement
Repository-Metriken
- Stars
- (48.455 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)
Beschreibung
🔎 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
💻 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.