remix-run / remix

Build Better Websites. Create modern, resilient user experiences with web fundamentals.
https://remix.run
MIT License
28.97k stars 2.45k forks source link

Type instantiation is excessively deep and possibly infinite.ts(2589) when using readonly #9776

Open punkpeye opened 1 month ago

punkpeye commented 1 month ago

Reproduction

https://stackblitz.com/edit/remix-run-remix-msgs25?file=app%2Froutes%2F_index.tsx

System Info

N/A

Used Package Manager

pnpm

Expected Behavior

No type error.

Actual Behavior

Getting error:

Type instantiation is excessively deep and possibly infinite.ts(2589)

punkpeye commented 1 month ago

The only code necessary to replicate is this:

export const loader = defineLoader(async () => {
  type RecentChat = {};

  const recentChats: readonly RecentChat[] = [];

  return {
    recentChats,
  };
});

const Route = () => {
  const data = useLoaderData<typeof loader>();
};
lilouartz commented 1 month ago

@brophdawg11 I am facing the same issue. This is a blocker for adopting turbo-stream. It forces me to continue using json. unstable_data has the same issue.

lilouartz commented 1 month ago

I dug into types. Looks like fixing this is super simple:

- type Serializable = undefined | null | boolean | string | symbol | number | Array<Serializable> | 
+ type Serializable = undefined | null | boolean | string | symbol | number | Array<Serializable> | ReadonlyArray<Serializable> | 
{
    [key: PropertyKey]: Serializable;
} | bigint | Date | URL | RegExp | Error | Map<Serializable, Serializable> | Set<Serializable> | Promise<Serializable>;
punkpeye commented 1 month ago

This fixed the issue!

lilouartz commented 2 weeks ago

@brophdawg11 @jacob-ebey Can this me merged upstream?

At the moment, I have to patch local node_modules every time I update Remix.

brophdawg11 commented 2 weeks ago

defineLoader is going away in favor of a better type story soon so this will be resolved once that lands

punkpeye commented 2 weeks ago

defineLoader is going away in favor of a better type story soon so this will be resolved once that lands

That's great news, but apps that currently rely on defineLoader cannot lint properly.

brophdawg11 commented 2 weeks ago

It's an unstable API not intended for production use. // @ts-expect-error is your friend here 😄