Skip to content
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

Incorrect inference of mapped substitution type in conditional involving a union of arrays (regression) #56018

Open
geoffreytools opened this issue Oct 7, 2023 · 1 comment Β· May be fixed by #56027
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@geoffreytools
Copy link

πŸ”Ž Search Terms

"inference", "conditional type", "substitution type", "mapped type", "type constraint", "array", "union type"

πŸ•— Version & Regression Information

This changed between versions 5.0.4 and 5.1.6

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.1.6#code/C4TwDgpgBAShB2BDAthAJlAvFAThRaA9vADYhQAUA3lANoDWAXFAAo6GQ6gDSEIAuswDOwHAEt4AcygBfKAB8oAV3hoIAMwnoAlLX4AofaEhQAYoUIAeACpQIAD2AI0Q3PiKlyFNhwhcQvOSKKmqa8Dp6CrAIKOgAfFj6UMlQtg5Oqq5wSKgYAPxQAOIQwIFClnAAjkpieGg2cQnMVTV1DYbG0MWlfOUwdo7OWTG5Cdg0tNxQElD0fITqsIKz84swk-yyhgD028pCiJIQRuDQAIJYZhaWtADkiLd5-Am7dABEiG8GnVAAQpfmKy0GiIPLMW4AI1usmeUFetA+XyAA

πŸ’» Code

type Renamed = readonly ({ [k: PropertyKey]: string } | undefined)[]

type Foo<T extends readonly (PropertyKey | undefined)[] | Renamed> =
    T extends Renamed ? GetKeys<Required<T>> : Required<T>
//                              ~~~~~~~~~~
// Type 'Required<T>' does not satisfy the constraint 'Renamed'

type GetKeys<R extends Renamed> = { [K in keyof R]: keyof R[K] }

// usage
type A = Foo<['a'?]> // ["a"]
type B = Foo<[{ a?: 'b' }]> // ["a"]

πŸ™ Actual behavior

TS fails to see that Required<T extends Renamed> extends Renamed.

πŸ™‚ Expected behavior

It should compile.

Additional information about the issue

Substitute readonly (PropertyKey | undefined)[] with something that is not an array and the problem disappears.

I get the same error when I do GetKeys<Id<T>> with type Id<T> = { [K in keyof T]: T[K] }.

Extract<Required<T>, Required<Renamed>> silences the error, but Extract<Required<T>, Renamed> does not.

@Andarist
Copy link
Contributor

Andarist commented Oct 7, 2023

I bisected it to this diff and to this PR. To be more precise - the adjustment from this PR to getResolvedApparentTypeOfMappedType broke this.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Oct 11, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants