Skip to content

Inference regression with mapped types in 3.4.1 #30868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JabX opened this issue Apr 11, 2019 · 1 comment · Fixed by #31027
Closed

Inference regression with mapped types in 3.4.1 #30868

JabX opened this issue Apr 11, 2019 · 1 comment · Fixed by #31027
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@JabX
Copy link

JabX commented Apr 11, 2019

TypeScript Version: Since 3.4.1, I had no issue in 3.4.0-rc and below

Search Terms:
Type inference regression in TS 3.4
(I know there are a couple issues on regressions introduced with TS 3.4 but they're not similar, and mine wasn't there in the RC)

Code

interface Entry {
   comment?: string;
}

interface Entity {
    fields: {[key: string]: Entry};
}

type Fields<E extends Entity> = {
    [P in keyof E["fields"]]: E["fields"][P]
};

type Nodes<T = any> = {
    [P in keyof T]: T[P] extends Entity
        ? Fields<T[P]>
        : T[P]
};

function makeEntityStore<T extends Record<string, Entity>>(config: T): Nodes<T> {
    return {} as Nodes<T>
}

const myTest = makeEntityStore({ test: { fields: { id: {} } } });
myTest.test

Expected behavior:
myTest.test's type should be Fields<{fields: {id: {}}>

Actual behavior:
myTest.test's type is Fields<Entity & {fields: {id: {}}>

This is the simplest repro I could manage for the bug I encounter in my project when I try to upgrade from the RC to any final version (3.4.1, 3.4.2 or 3.4.3, and it's still there in the latest nightly I tried). This might look inoffensive presented like that, but in the "real world" the types presented here are way more complex and that added intersection with the constraint type messes up literally every usage I get from that function. I have to stay on the RC because of this.

Actually, there is a very simple tweak you can do to this exemple to get the expected inference behavior, which is removing the comment?: string property on Entry. Obviously, this isn't an acceptable solution, but it makes all of that look even more like a tiny bug that made its way into the compiler during the finalization of the 3.4 version.

Playground Link: Here

Playground result
image

Playground result with tweak
image

@JabX
Copy link
Author

JabX commented Apr 11, 2019

I tried to pinpoint when exactly this bug appeared, and it looks like the first nightly to show this behaviour is typescript@3.4.0-dev.20190328, which points to the #30592 PR if the diff is to be believed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
3 participants