microsoft / microsoft/TypeScript

Declaring index signature based on property type gives invalid error

Open
#30,982 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Indexed Access Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: latest, demonstrable on playground (links below)

Search Terms: index signature diagnostics property type

Code:


interface BaseNode {
  kind: string;
  [key: string]: string | TokenNode["data"];
}

interface TokenNode extends BaseNode {
  kind: 'token';
  data: TokenData;
}

interface TokenData {
  offset: number;
  width: number;
  leading_width: number;
  trailing_width: number;
}

Expected behavior:

No errors, as the code is correct.

Actual behavior:

An error on interface TokenNode:

Interface 'TokenNode' incorrectly extends interface 'BaseNode'.
  Index signature is missing in type 'TokenNode'.

HOWEVER if I change TokenNode["data"] to its value TokenData, the error goes away.

Playground Link:

Code above displaying the error
Code above with the fix, no errors

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 both linked TypeScript Playground examples, comparing the indexed-access form with the equivalent TokenData form. Trace the type-checking behavior for the BaseNode and TokenNode interface declarations; done means the indexed-access version no longer reports the invalid inheritance error while the existing valid case remains error-free.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.