Skip to content

Conditional type eagerly calculated to be invariant breaks referential transparencyΒ #62798

@devanshj

Description

@devanshj

πŸ”Ž Search Terms

Conditional types, Invariance, Referential transparency

πŸ•— Version & Regression Information

Tried with v5.9.3

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/C4TwDgpgBAYgPAFQHxQLxQN5QG4EMA2ArhAFxQJQQAewEAdgCYDOUA9gEYBWEAxsFAH4oAawghWAM3JQyFAL4AoBaEhQASgEY0sOBjkpqtRi3hZcZJsABOASzoBzKPsFQARAAtW+Zq5luGrBAsdKz8nt6uCgD0UVBxAHoCSirQMLjapvrK4KnsGbpQ5lCWtg5OSMk56gBMGemG9MyweUIeXj5+rgFBUCFh7ZExcVCJCkA

πŸ’» Code

type F<T> = { value: T extends object ? keyof T : T }

type R1 = F<{}> extends F<{ a: string }> ? "holds" : "does not hold"
//   ^? "does not hold"

type Fa = F<{}>
type Fb = F<{ a: string }>

type R2 = Fa extends Fb ? "holds" : "does not hold"
//   ^? "holds"

πŸ™ Actual behavior

R1 computes to "does not hold"

πŸ™‚ Expected behavior

R1 computes to "holds"

Additional information about the issue

I'm assuming typescript eagerly calculates F to be invariant and then instead of checking the subtype relation on resolved types it calculates subtype relation of the parameters to F. (This would perhaps have made sense if F was explicitly annotated with in out T.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptCursed?It's likely this is extremely difficult to fix without making something else much, much worseHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions