Skip to content

Generic type constraint doesn't seem to propagate to conditional typeΒ #56045

Closed
@Bubblyworld

Description

@Bubblyworld

πŸ”Ž Search Terms

"generic constraint conditional type"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types and generics

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgzgeQEYCsIGNgB4AqA+KAXihyggA9gIA7AEzigHtUNgoB+EqALimogBuEAE4BuAFAAzAK7VMAS0bUok6rjKUa9Ji0x4AFMF44AlFADe4qFHRK4bcr3jI0mXAWLAJAXyA

πŸ’» Code

type IsObject<T> = T extends object ? T : never;
function fn<T extends object>(t: T) {
  const x: IsObject<T> = t; // error 2322
}

πŸ™ Actual behavior

  1. Type 'T' is not assignable to type 'IsObject'.
    Type 'object' is not assignable to type 'IsObject'. [2322]

πŸ™‚ Expected behavior

Since the type parameter T is constrained to extend object, I expected tsc to infer that T is a subtype of IsObject<T>.

I'm sure I'm misunderstanding how constraints or conditional types work, but on the face of it this is very surprising to me. In the real world version of this I'm trying to to construct generic queries with MikroORM, which does an IsObject<T> check recursively in its query DSL type.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions