Next.js Discord

Discord Forum

Nextjs Build in TypeScript Monorepo; Types not inferred correctly

Unanswered
Pteromalid wasp posted this in #help-forum
Open in Discord
Avatar
Pteromalid waspOP
I have a TS Monorepo using Yarn workspaces. I have a shared db package which which also exports shared types for the database entities.
The types from @shared/types do not get inferred correctly in the web app. The db package uses drizzle and drizzle utility type helpers to create type.

For example: If I have a users table and I want to infer the user entity, I am using this:

export type User = typeof users.$inferInsert;

In the db package itself, it works fine but in the web package, User gets inferred as any
but if I am manually creating a type, it gets inferred correctly. The following gets correct inference:

type User = { id: string, name: string, email: string, createdAt: Date }
// OR
type Position = { x: number, y: number }

During development, if I use TS References, it correctly infers the types but it is not supported by Next and so it fails on build.

Here' the folder structure

apps
   web (nextjs app)
      package.json
           dependencies: '@my-app/db'
packages
    db
       types
          index.d.ts (exports every d.ts files)
          package.json (name: '@my-app/db')

package.json (root level package json)
    packageManager: "yarn@4.5.3"
    "workspaces": [
        "apps/*",
        "packages/*"
    ],


Also, I've also already tried transpiling teh shared packages usign traspilePackages in Next Config. It does not work.

0 Replies