microsoft / microsoft/TypeScript

Partial of a mapped type from a union of enums fails when these enums have common members

Open
#41,700 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 4.0.5, 4.1.2, 4.2.0-dev.20201126

Works fine with TS 3.X

Search Terms: mapped type enum partial

Code

enum EnumA {
    A = 'A',
    B = 'B',
}

// A second enum with at least one key also in EnumA
enum EnumB {
    B = 'B',
    C = 'C',
}

type Mapped = {
    [k in EnumA|EnumB]: string;
};

const partial: Partial<Mapped> = {
    [EnumA.B]: 'value',
};

Expected behavior:
Should compile

Actual behavior:
Typescript complains that EnumA.B does not exist in Partial<Mapped> when declaring the partial variable:

Type '{ B: string; }' is not assignable to type 'Partial<Mapped>'.
  Object literal may only specify known properties, and '[EnumA.B]' does not exist in type 'Partial<Mapped>'.

Playground Link: https://www.typescriptlang.org/play?ts=4.2.0-dev.20201126#code/KYOwrgtgBAouEEEoG8BQUNSQXigcgTwBp1MAhKXPM41AX1VVElngrUygqppM4GFK+frQaoALgE8ADsCgBZAIbTZAEyEdMAbQDWUAJYhWkBAB84kMgF0AXFADO4gE6GA5gG56n1AGMA9iCOUADu+uIAFn5g4vx+EBABAGqKADZgwHYACopO4vqpADxKKsCqAHwapBhaFogAdAi2+LScNWx1-E14ot7+geJQ-vFJqekAYk5xCFk5eYXFahW4mtW1CHXWdt0kdL0BQUMJIMlpwBNxZDO5+SlFyouVrbVkG109jH1BoRFR4tnXqTsC1Kj20awaXXWhD4YPg602+HhLVhlleWxevCqUDaqM66I67yAA

Related Issues: maybe https://github.com/microsoft/TypeScript/issues/37859 ?

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

Begin with the linked TypeScript Playground and the supplied enum, mapped-type, and Partial repro, then compare the reported behavior across the listed TypeScript versions. Done means the example compiles as expected when the mapped type is built from the union of enums, with regression coverage for the shared enum member case.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.