question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The results of the 'Mapped Types' distribution have changed.

See original GitHub issue

Bug Report

The conversion result of ‘Mapped Types’ has changed in 4.3.2 or later typescript.

💻 Code

type Base = { 100: { a: number }; 200: { b: string } }
type NewType<T = Base> = {
  [P in keyof T ]: {
    code: P
    body: T[P]
  }
} extends {
  [P in any]: infer R
}
  ? R
  : never

let test: NewType

🙁 Actual behavior

  • 4.3.0-beta and earlier
let test: {
    code: 100;
    body: {
        a: number;
    };
} | {
    code: 200;
    body: {
        b: string;
    };
}
  • 4.3.2
let test: {
    code: keyof Base;
    body: {
        a: number;
    } | {
        b: string;
    };
}

🙂 Expected behavior

Hoping to return to pre-4.3.0-beta behavior!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vain0xcommented, May 31, 2021

Another workaround (tried on ts 4.4.0-dev.20210530):

type NewType<T> = {
  [P in keyof T]: {
    code: P
    body: T[P]
  }
}[keyof T]

type Base = { 100: { a: number }; 200: { b: string } }
declare const test: NewType<Base>
0reactions
weswighamcommented, Jul 25, 2021

I think our syntactic inference constraint rules are kiiiiiinda arbitrary to begin with (what type parameters we match up across what locations has kinda been chosen by request) - I “fixed” the issue by adding one because the change in behavior was technically a regression, and adding those syntactic constraints is sort-of free to do. The extra instantiation would probably be the best thing we could do to keep the old behavior going. We could back it out… But then we’d be reintroducing the old regression…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapped Types should distribute over union types · Issue #28339
At least the title should be changed from "Mapped types" to "Mapped types that are a function of keys ". Otherwise it is...
Read more >
Typescript: mapped types, strip field from a union
This means that the mapped type is applied independently to each member of the union and all results are unioned into the final...
Read more >
KB5014754—Certificate-based authentication changes on ...
In general, mapping types are considered strong if they are based on identifiers that you cannot reuse. Therefore, all mapping types based on...
Read more >
Exploring TypeScript Mapped Types Together | HackerNoon
What works for me when building complex mapped types is starting simple and then incrementally adding complexity.
Read more >
3.2 Thematic Maps | GEOG 160: Mapping our Changing World
A wide array of map types has been developed over the years to represent ... The goal of choropleth maps is to depict...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found