encore icon indicating copy to clipboard operation
encore copied to clipboard

typeof inferSelect not supported

Open guigaribalde opened this issue 1 year ago • 11 comments

CleanShot 2024-12-22 at 23 02 07@2x

const ExampleTable = pgTable('example', t => ({
  id: t.uuid().notNull().primaryKey().defaultRandom(),
  example: t.varchar({ length: 256 }).notNull(),
}));

type Example = InferSelectModel<typeof ExampleTable>; // this cannot be used as an api type

When using drizzle $inferSelect i get the message: typeof with non-ident not yet supported... Any workarounds while this feature is not here?

guigaribalde avatar Dec 23 '24 02:12 guigaribalde

In the drizzle example they just hint at using your own DTOs, which is sub-optimal but it should work for now.

saguirre avatar Dec 25 '24 15:12 saguirre

Yes, is the same case for Prisma.

luisnquin avatar Dec 25 '24 18:12 luisnquin

yeah, I hope this issue get more attention

ImBIOS avatar Feb 05 '25 06:02 ImBIOS

I am having the same problem

hariangr avatar Mar 17 '25 07:03 hariangr

Running into the same issue using Drizzle, I'd love to see this working in the future. Having to declare the same type twice is a bit of a bummer since this is not something that I would have to do with other frameworks that I'd like to replace with Encore.ts.

ecarlste avatar Mar 20 '25 21:03 ecarlste

Same issue (I believe) also when using Valibot to define requests (for validation purposes) and using InferInput<typeof MyRequestSchema> to get the type definition for Encore.

jukkatawast avatar Apr 20 '25 19:04 jukkatawast

Why this issue is not getting any attention? While working on a large project it's painful to manage manual DTOs... I prefer drizzle-zod, z.infer<typeof...> why wouldn't it support it?

raikusy avatar May 13 '25 07:05 raikusy

Why this issue is not getting any attention? While working on a large project it's painful to manage manual DTOs... I prefer drizzle-zod, z.infer<typeof...> why wouldn't it support it?

I do agree. Currently this is the main reason why I am not using encore in prod...

guigaribalde avatar May 13 '25 13:05 guigaribalde

You can use DTOs just fine with Encore, you just can't expose them as part of the public API of your Encore endpoints. This is something we're working towards supporting, but it will take some time as we need to add support for more features of the TypeScript type system first.

That said, it's generally not a good idea to expose your database schema in your API, as you typically want them decoupled for security reasons anyway, and to have more fine-grained control over your API. In my experience defining a few additional types to represent your public API is not a huge lift in terms of effort, and also leads to better API design and better API documentation.

eandre avatar May 13 '25 16:05 eandre

@eandre it's good to know that you're working on it! Just want to mention that even manually defining types for the public API doesn't always work:

const distanceUnits = ["m", "km", "mi"];

type TDistanceUnit = (typeof distanceUnits)[number];

Using TDistanceUnit in the public API of an encore endpoint fails with this error:

error: unsupported indexed access type operation: obj Union(
    Union {
        types: [],
    },
) index Basic(
    Number,
)
 --> /path/to/hello.ts:9:22
  |
9 | type TDistanceUnit = (typeof distanceUnits)[number];
  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Hopefully this will be covered by the work you're already doing 😃 .

CodeWithOz avatar May 24 '25 17:05 CodeWithOz

Yes, is the same case for Prisma.

So is there any viable solution available now?

Edge-coordinates avatar Sep 24 '25 01:09 Edge-coordinates