microsoft / microsoft/pxt-microbit

Dictionary indexing type error

Open
#3,382 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

apis compiler P2 python
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.