microsoft / microsoft/TypeScript
Type Instantiation is Excessively Deep Regression In #56004
@ahejlsberg is already working on this.
Since Aug 19, 2024.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
Type Instantiation is Excessively Deep, #56004
🕗 Version & Regression Information
This changed in commit 38ef79e0b02ad122b984a19f3494414e4e53c896
⏯ 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 anyis required because #56004 only operates in cases where the type parameter is constrained.BodyChecksHeritageandConstraintChecksHeritageare both required to increase the depth.
But that leaves some things I can't personally explain such as:
- Why is
a?: thiscausing issues buta: this | undefinedor others don't necessarily? - Why does
PlaceableObjecthave to be generic? - etc.
I've noted more oddities inline with the code.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.