-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New error: Type instantiation is excessively deep and possibly infinite. #37613
Comments
3.9 errors:
|
I took a quick look. The excessively depth errors are caused by these two types in type TransitionsConfigMap<TContext, TEvent extends EventObject> = {
[K in TEvent['type'] | NullEvent['type'] | '*']?: SingleOrArray<
| TransitionConfigTargetShortcut<TContext, TEvent>
| (TransitionConfig<
TContext,
K extends TEvent['type'] ? Extract<TEvent, { type: K }> : EventObject
> & {
event?: undefined;
})
>;
};
type TransitionsConfigArray<TContext, TEvent extends EventObject> = Array<
{
[K in TEvent['type'] | NullEvent['type'] | '*']: TransitionConfig<
TContext,
K extends TEvent['type'] ? Extract<TEvent, { type: K }> : EventObject
> & {
event: K;
};
}[TEvent['type'] | NullEvent['type'] | '*']
>; The problem appears to be exploration of the conditional type constrains that keep spiraling until we hit the depth limiter. When I change |
FYI @davidkpiano |
Working on a fix for this, but I found some odd type behavior: type ResolvedEvent = ({
type: "CHANGE";
value: string;
} & {
type: "CHANGE";
}) | ({
type: "SAVE";
} & {
type: "CHANGE";
})
function assign(event: ResolvedEvent) {
return event.value;
} This will produce the following error for
I would expect |
Are you sure you're using the 3.9 compiler? There's no error with the nightly build: |
Ah, no, I was using 3.8.3. Glad to see it's fixed - was that specific issue tracked? |
Yes, it was fixed by #36696. |
I'm facing the error in such a case: type GetLetters<Text> = Text extends `${infer Letter}${infer Rest}` ? Letter | GetLetters<Rest> : never;
type Letters = GetLetters<"some very, very long text">; The error appears when the text is longer than or equal to 24 characters. |
@vasilii-kovalev We currently have a limit of 50 nested type instantiations, which in your case equates to 24 characters because each level consists of a conditional type and a union type. A good workaround is to "batch" for some number of characters. For example, this processes characters in batches of 10: type GetLetters<Text> =
Text extends `${infer C0}${infer C1}${infer C2}${infer C3}${infer C4}${infer C5}${infer C6}${infer C7}${infer C8}${infer C9}${infer Rest}` ? C0 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | GetLetters<Rest> :
Text extends `${infer C}${infer Rest}` ? C | GetLetters<Rest> : never; This should allow you to process strings of up to 250 characters. Our currently limit of 50 is probably a bit low now that we have template literal strings and we're looking at increasing it. However, there will always be some limit to how deep you can nest recursive types given that Node.js defaults the call stack size to 1Mb or less. |
@ahejlsberg, thank you for the explanation! I have a library, which depends on the approach I described above. // "Hello, John!"
console.log(
hydrateText(
"Hello, {username}!",
// No errors
{ username: "John" },
{
prefix: "{",
suffix: "}",
},
),
);
console.log(
hydrateText(
"Hello, {username}!",
// Error: `username` is missing
{},
{
prefix: "{",
suffix: "}",
},
),
);
// "{U}<S>(E)[R]"
console.log(
hydrateText(
"user",
{
u: "{U}",
s: "<S>",
e: "(E)",
r: "[R]",
},
{
prefix: "",
suffix: "",
},
),
); I've created a playground with types only, that shows how the limitation affects the library's behavior. I don't think that the "batching" can help a lot in this case, because in order to "batch" even two variables, it would be necessary to significantly complicate the P.S.: I've noticed some strange behavior with the error. In the playground above, it is hidden until |
This one's actually new in 3.8. There are some additional errors in 3.9 - I'll post them below.
https://github.com/davidkpiano/xstate/blob/2e0bd5dcb11c8ffc5bfc9fba1d3c66fb7546c4bd/packages/core/src/stateUtils.ts#L157
To repro:
yarn
tsc -b -f tsconfig.monorepo.json
The text was updated successfully, but these errors were encountered: