microsoft / microsoft/TypeScript

Incorrect type inference for Record with symbol key

Open
#37,290 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: Nightly

Search Terms: Record, Symbol, symbol as index

Code

export type SN = Record<symbol, number>

const s = Symbol()
const sn: SN = {}

const n = sn[s] // n is of type any instead of number

Expected behavior:

A Record with symbols as key should allow to access properties by symbol with correct type

Actual behavior:

All keys are any type.

Playground Link: http://www.typescriptlang.org/play/?noImplicitAny=false&strictNullChecks=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&ts=3.9.0-dev.20200308#code/KYDwDg9gTgLgBDAnmYcDKA5OBeOAlYAY2gBMAeAZ0QFsAjCAGwBo4A7AVzuCgD4AoPsVYV4FHOhr0GACgCUgiMNGsAXOiy4A3gF8BQkW3EVWAbQoBdIA

Related Issues: #1863

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 example in the issue using the TypeScript Nightly playground or the shown TypeScript version, then trace the compiler's handling of indexed access on Record<symbol, number>. Compare the inferred type of sn[s] with the expected number and use the related issue #1863 for context. Done means symbol-keyed Record access is inferred as number rather than any, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.