Closed
Description
π 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
π» 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
- 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