Skip to content

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

Open
@ssalbdivad

Description

@ssalbdivad

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions