microsoft / microsoft/TypeScript

Broken inference between index typed object and Record

Open
#14,930 4 comments 2 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: 2.2.2

Code

interface Errors {
  [field: string]: {
    key: string
    message: string
  }
}

const errors: Errors = {}

function genericObjectFunction<K extends string, V>(obj: Record<K, V>): [K, V][] {
  return []
}

/*
 Argument of type 'Errors' is not assignable to parameter of type 'Record<string, never>'.
 Index signatures are incompatible.
 Type '{ key: string; message: string; }' is not assignable to type 'never'.
 */
genericObjectFunction(errors)

The types seem generally compatible though, as expected this is legal:

const errorRecord: Record<string, { key: string, message: string }> = errors

Expected behavior:
genericObjectFunction(errors) should compile.

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 minimal TypeScript 2.2.2 reproduction in the issue, focusing on genericObjectFunction and inference from the Errors index signature to Record. Done means genericObjectFunction(errors) compiles while preserving the demonstrated Record compatibility.

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.