Open
Description
π Search Terms
Type Instantiation is Excessively Deep, #56004
π Version & Regression Information
This changed in commit 38ef79e
β― Playground Link
π» Code
type BodyChecksHeritage<O> = O extends PlaceableObject ? O : never;
type ConstraintChecksHeritage<O extends PlaceableObject> = any;
// Name must extend something i.e. `GetConfigured<Name> = ...` compiles but this doesn't.
type GetConfigured<Name extends any> = ConstraintChecksHeritage<
BodyChecksHeritage<
// Changing this to `Name extends "AmbientLight" ? AmbientLight : Token` fixes the compilation.
InstanceType<
Name extends "AmbientLight" ? typeof AmbientLight : typeof Token
>
>
>;
// A type parameter is required for this compilation failure. It can be used and still fail to compile but if it's removed entirely it compiles.
declare class PlaceableObject<Unused = any> {
// To fail to compile must contain an optional property that refers to `this`.
a?: this;
// This property exists to differentiate from the type `{}` but it isn't actually necessary to cause the compilation failure.
#notEmpty: true;
}
// To show this compilation failure the class could literally be just `class AmbientLight {}`. It has to exist though.
declare class AmbientLight extends PlaceableObject {
#notEmpty: true;
}
declare class Token extends PlaceableObject {
// Must refer to another property. `this` alone isn't enough.
b: this["c"];
// Only exists to be referred to. Technically you could remove this and the "excessively deep" error doesn't go away.
// However that causes another compilation error.
c: number;
}
π Actual behavior
Gets the error "Type instantiation is excessively deep and possibly infinite."
π Expected behavior
No error.
Additional information about the issue
I've seen somewhat similar issues reported about this PR but none of them seemed to match my issue. However some were closed without reproduction and maybe I'm having the same problem as type-fest is?
I've reduced the code down from a large project into this which is why it seems utterly contrived. A few of the odd requirements seem to be explainable:
Name extends any
is required because Improve constraints of conditional types applied to constrained type variablesΒ #56004 only operates in cases where the type parameter is constrained.BodyChecksHeritage
andConstraintChecksHeritage
are both required to increase the depth.
But that leaves some things I can't personally explain such as:
- Why is
a?: this
causing issues buta: this | undefined
or others don't necessarily? - Why does
PlaceableObject
have to be generic? - etc.
I've noted more oddities inline with the code.