Skip to content
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

Excessive stack depth comparing types 'FlatArray<Arr, ?>' and 'FlatArray<Arr, ?>'. #43249

Closed
RomanKornev opened this issue Mar 13, 2021 · 16 comments · Fixed by #43435
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@RomanKornev
Copy link

TS and JS Grammar Extension version: ms-vscode.vscode-typescript-next v4.3.20210312
Trying to use configureStore with middlewares, getting the following error:
image
A couple days ago it was fine.
Also getting a lot of similar Excessive stack depth errors for other parts of the code.
Code

import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'

configureStore({
    reducer: {
    },
    middleware: [
        ...getDefaultMiddleware(),
    ],
})
@kudryavtsev-dmitry
Copy link

kudryavtsev-dmitry commented Mar 15, 2021

I changed TS version from vscode version 4.3.0 to workspace 4.2.3, and this fixed my problem.

image

@utkarshk384
Copy link

@kudryavtsev-dmitry Thanks bud, you saved my day :). Been getting this error all over my react project for almost a week! :(

@ghost
Copy link

ghost commented Mar 15, 2021

Typescript nightly for vscode throw this error v4.3.20210314

@sheetalkamat sheetalkamat transferred this issue from microsoft/TypeScript-TmLanguage Mar 15, 2021
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 15, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.0 milestone Mar 15, 2021
@andrewbranch
Copy link
Member

I have a simple fix, but I don’t think it gets to the core of the issue. Via bisecting, I found that the problem was introduced in #30639 (cc @weswigham). Here’s a minimal repro:

// @lib: es2019
interface MiddlewareArray<T> extends Array<T> {}
declare function configureStore(options: { middleware: MiddlewareArray<any> }): void;

declare const defaultMiddleware: MiddlewareArray<any>;
configureStore({
  middleware: [...defaultMiddleware], // Should not error
});

The problem starts when we need to know if defaultMiddleware is an array-like type. We can’t take the preferred shortcut of finding that it’s a simple reference to Array or ReadonlyArray since it’s a MiddlewareArray, so we start checking assignability to readonly any[], and then we overflow while comparing each array’s .flat() call signatures.

I haven’t yet dug into why #30639 causes that comparison to overflow, but it seemed like we should really never have to do that work at all—if a type has Array or ReadonlyArray as one of its base types, I think we already know that it’s array-like. I have that fix ready to go if we want it, but we should probably figure out if we need another fix for conditional type assignability.

@andrewbranch
Copy link
Member

Confirmed my isArrayLike change fixes the repro in #43258 too.

@jonlepage
Copy link

confirm back to 4.2.3 work ! .
4.3.0> trow this error.
Excessive stack depth comparing types 'FlatArray<Arr, ?>' and 'FlatArray<Arr, ?>'.ts(2321)
image

@danielo515
Copy link

In my case it is happening using recharts recharts@1.8.1 and typescript@4.2.3

image

@junhoyeo
Copy link

I was having the same issue with typescript@4.2.3, and then I noticed that I wasn't wrapping the mapped result into a Fragment. Now I have no errors 😄

+ <>
  {data.feeds.map((item, index) => {
    return <div key={someKey}>{JSON.stringify(item)}</div>
  })}
+ </>

@danielo515
Copy link

I was having the same issue with typescript@4.2.3, and then I noticed that I wasn't wrapping the mapped result into a Fragment. Now I have no errors 😄

+ <>
  {data.feeds.map((item, index) => {
    return <div key={someKey}>{JSON.stringify(item)}</div>
  })}
+ </>

But this is a perfectly valid react pattern, you should not need that as soon as you have a parent component

@Jack-Works
Copy link
Contributor

Having this problem in 4.3.0-dev.20210327 in many JSX code

@earthlyreason
Copy link

earthlyreason commented Mar 30, 2021

I ran into this when trying the 4.3.0-dev.20210329 nightly with code that destructures RegExp matches. Here is the minimal repro I could find (Playground link):

const repro_43249 = (value) => {
  if (typeof value !== "string") {
    throw new Error("No");
  }

  const match = value.match(/anything/) || [];

  const [extracted] = match;
  //     ~~~~~~~~~^ Excessive stack depth comparing types 'FlatArray<Arr, ?>' and 'FlatArray<Arr, ?>'
};

You can reproduce this with the following tsconfig.json:

{
  "compilerOptions": {
    "lib": ["ES2019"]
  }
}

Curiously,

  • it only occurs with lib ES2019 and above
  • it goes away if you get rid of the throw
  • it goes away if you change the typeof check to anything else

I notice that npx tsc test would not produce it, but npx tsc -p tsconfig.json would.

@Argiziont
Copy link

Argiziont commented Mar 30, 2021

<></>

This attributes works perfectly but it's unnecessary as pointed out before

@zackdotcomputer
Copy link

Trying to understand the current state of this bug. It's still occurring as of the 3/30 nightly - I just ran into with a string interpolation in JSX and was able to fix with the "wrap it in a React.fragment" as suggested.

Is there currently a PR open for a fix or are we at least vaguely sure of why this is happening? (I know @andrewbranch mentioned having a possible solution above.)

@andrewbranch
Copy link
Member

Found a repro that is not fixed by #43435:

function f<Arr, D extends number>(
  x: FlatArray<Arr, any>,
  y: FlatArray<Arr, D>
) {
  x = y;
  y = x;
}

@DetachHead
Copy link
Contributor

so did i, though it may be the same thing #43493

@ahejlsberg
Copy link
Member

With #43527 we should have this issue taken care of for good.

karrui added a commit to opengovsg/FormSG that referenced this issue Jun 7, 2021
zod is incompatible with Typescript 4.3.x due a change in 4.3 for evaluating deep complex types.

Since we do not use any 4.3 features (yet), lock typescript package to 4.2 until this is fixed

see microsoft/TypeScript#43249, colinhacks/zod#443, colinhacks/zod#473, microsoft/TypeScript#44299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.