remix-run / remix

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

unstable_defineLoader can't handle types with arrays #9527

Closed brookslybrand closed 4 months ago

brookslybrand commented 4 months ago

Reproduction

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

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @remix-run/dev: * => 2.9.2 
    @remix-run/node: * => 2.9.2 
    @remix-run/react: * => 2.9.2 
    @remix-run/serve: * => 2.9.2 
    vite: ^5.1.0 => 5.2.12

Used Package Manager

npm

Expected Behavior

Expected types to work with a interface/type with arrays, such as the following:

interface BlogPost {
  title: string;
  authors: { name: string }[];
}

async function getBlogPost(): Promise<BlogPost> {
  return Promise.resolve({
    title: 'example blog',
    authors: [{ name: 'brooks' }], // This seems to be the problem
  });
}

export const loader = defineLoader(async () => {
  const blog = await getBlogPost();
  return { blog };
});

If you update getBlogPost to infer it's return type, everything works. However, this is not ideal, since that might require changing a lot of types, plus some people don't want their functions to infer types, they'd rather declare the return type.

Additionally, the type error message is pretty dense. For useLoader, the message is just

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

Actual Behavior

unstable_defineLoader accepts types with arrays

brookslybrand commented 4 months ago

@pcattori here's a screenshot of the type error this gives me

image
KubaJastrz commented 4 months ago

Duplicate of #9514

brookslybrand commented 4 months ago

Thanks @KubaJastrz, closing in favor of your issue