microsoft / microsoft/TypeScript

use [""] in object will lose ts check

Open
#58,908 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: LS: Quick Info Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms
          This seems to be a TypeScript bug/limitation:

TypeScript Playground

Originally posted by @KermanX in https://github.com/vuejs/language-tools/issues/3819#issuecomment-2173682276

🕗 Version & Regression Information
  • This is a crash
  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/FAYw9gdgzgLgBAQwFxwEoFNwCcAmAeWLASwgHMAaOCAVwFsAjdLAPjgDI4BvRARhR7gBfOAF4uQxFEQQAnsGAIAdAh7AA9GrgA9APwKA2gCIVhgLrrNuhcoBMF7XoRGENs3A1wAcgHk4ACQBJTwAVAEJ7KyA

💻 Code

const a: Record<string, number> & { a1: 1 } = { } as any

a.a1
// ^?
a["a1"]
// ^?
a.a2
// ^?
a["a2"] // NO HINT!
// ^?

🙁 Actual behavior

a["a2"] // NO HINT!

🙂 Expected behavior

a["a2"] should be hint by typescript

Additional information about the issue

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and reproduce the difference between a.a2 and a["a2"] using the Record<string, number> intersection shown in the issue. Trace how these two property-access forms are checked and identify why the bracket form produces no hint. Done means a["a2"] receives the expected TypeScript hint consistently with a.a2.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.