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

Computed type key inference inconsistent with identical explicitly declared type #59987

Open
ssalbdivad opened this issue Sep 17, 2024 · 2 comments Β· May be fixed by #59995
Open

Computed type key inference inconsistent with identical explicitly declared type #59987

ssalbdivad opened this issue Sep 17, 2024 · 2 comments Β· May be fixed by #59995
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@ssalbdivad
Copy link

πŸ”Ž Search Terms

keyof required keys key evaluation reduce getReducedType

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://tsplay.dev/WKQkzN

πŸ’» Code

type distilled = distill<{
	//   ^?
	foo: "default"
	bar?: number
}>

// incorrectly inferred as unknown
type requiredDistilledKey = requiredKeyOf<distilled>
//   ^?

// explicit declaration identical to hover of distilled
type declared = {
	bar?: number
} & {
	foo?: "default"
}

// now correctly inferred as never
type requiredDeclaredKey = requiredKeyOf<declared>
//   ^?

type requiredKeyOf<o> = {
	[k in keyof o]-?: o extends { [_ in k]-?: o[k] } ? k : never
}[keyof o]

type distill<t> = t extends object ? distillMappable<t> : t

type distillMappable<o> = {
	[k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: distill<o[k]>
} & {
	[k in inferredDefaultKeyOf<o>]?: distill<o[k]>
}

type inferredDefaultKeyOf<o> = {
	[k in keyof o]: o[k] extends "default" ? k : never
}[keyof o]

πŸ™ Actual behavior

requiredDistilledKey inferred as unknown

πŸ™‚ Expected behavior

requiredDistilledKey inferred as never, consistent with the equivalent declared version

Additional information about the issue

No response

@ssalbdivad ssalbdivad changed the title Keys of computed type inferred inconsistently with the same type when explicitly declared Computed type key inference inconsistent with identical explicitly declared type Sep 17, 2024
@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 17, 2024

Duplicate of #59948. Without exactOptionalPropertyTypes the added | undefined makes no difference, and with exactOptionalPropertyTypes enabled the types are identical.

Andarist corrected me. πŸ‘

@Andarist
Copy link
Contributor

It's not a duplicate of that other issue, the other one is purely about how types are displayed. This one here is an actual functional difference between those 2, seemingly, identical types.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Sep 19, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 19, 2024
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.

4 participants