microsoft / microsoft/pxt-microbit
Dictionary indexing type error
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 804
- Forks
- 721
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 2
Description
Describe the bug
In Python:
Indexing a str->int dict with string variables causes an error. Indexing with a string directly works.
To Reproduce
d = {'x': 1}
x = 'x'
d['x'] = 2
d[x] = 2 # Offending line
Error: Line 4: Element implicitly has an 'any' type because type '{ "x": number; }' has no index signature.
Same thing happens if I add : str to definition of x.
Expected behavior
Dictionary lookup to work, even in this case.
Desktop (please complete the following information):
- OS: Linux
- Browser: Mozilla Firefox 80.0.1
Additional context
Happens on master branch, and on live website.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported snippet on the live website or master branch and compare direct string indexing with indexing through the string variable. Trace the TypeScript dictionary/index-signature diagnostic to identify the relevant compiler or editor entry point; done means the variable-based lookup is accepted without the reported error and the existing behavior remains covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100