Skip to content

Commit

Permalink
Solve the Prisma type issues by converting interfaces to types
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Aug 20, 2024
1 parent 7ddd6a7 commit c3ca958
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from "../_types";
import type { Prisma } from "@prisma/client";
import type { Payload } from "../_types/serialization";
import type { RecursiveInterfaceToType } from "../../universal/types";
import type {
{= crud.entityUpper =},
} from "wasp/entities";
Expand Down Expand Up @@ -79,7 +80,7 @@ export type GetAllQueryResolved = typeof _waspGetAllQuery

{=# crud.operations.Get =}
{=^ overrides.Get.isDefined =}
type GetInput = Prisma.{= crud.entityUpper =}WhereUniqueInput
type GetInput = RecursiveInterfaceToType<Prisma.{= crud.entityUpper =}WhereUniqueInput>
type GetOutput = _WaspEntity | null
export type GetQueryResolved = {= crud.name =}.GetQuery<GetInput, GetOutput>
{=/ overrides.Get.isDefined =}
Expand All @@ -91,10 +92,10 @@ export type GetQueryResolved = typeof _waspGetQuery

{=# crud.operations.Create =}
{=^ overrides.Create.isDefined =}
type CreateInput = Prisma.XOR<
type CreateInput = RecursiveInterfaceToType<Prisma.XOR<
Prisma.{= crud.entityUpper =}CreateInput,
Prisma.{= crud.entityUpper =}UncheckedCreateInput
>
>>
type CreateOutput = _WaspEntity
export type CreateActionResolved = {= crud.name =}.CreateAction<CreateInput, CreateOutput>
{=/ overrides.Create.isDefined =}
Expand All @@ -106,11 +107,11 @@ export type CreateActionResolved = typeof _waspCreateAction

{=# crud.operations.Update =}
{=^ overrides.Update.isDefined =}
type UpdateInput = Prisma.XOR<
type UpdateInput = RecursiveInterfaceToType<Prisma.XOR<
Prisma.{= crud.entityUpper =}UpdateInput,
Prisma.{= crud.entityUpper =}UncheckedUpdateInput
>
& Prisma.{= crud.entityUpper =}WhereUniqueInput
& Prisma.{= crud.entityUpper =}WhereUniqueInput>

type UpdateOutput = _WaspEntity
export type UpdateActionResolved = {= crud.name =}.UpdateAction<UpdateInput, UpdateOutput>
Expand All @@ -123,7 +124,7 @@ export type UpdateActionResolved = typeof _waspUpdateAction

{=# crud.operations.Delete =}
{=^ overrides.Delete.isDefined =}
type DeleteInput = Prisma.{= crud.entityUpper =}WhereUniqueInput
type DeleteInput = RecursiveInterfaceToType<Prisma.{= crud.entityUpper =}WhereUniqueInput>
type DeleteOutput = _WaspEntity
export type DeleteActionResolved = {= crud.name =}.DeleteAction<DeleteInput, DeleteOutput>
{=/ overrides.Delete.isDefined =}
Expand Down
17 changes: 17 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/universal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,20 @@ export type Tail<T extends [unknown, ...unknown[]]> = T extends [unknown, ...inf

// Source: https://stackoverflow.com/a/55541672
export type IfAny<Value, Then, Else> = 0 extends (1 & Value) ? Then : Else;

// Prisma generated types which we use as default input and output types for CRUD
// operations internally use interfaces for some types.
// Our SuperJSON serialization types throw a type error when used with interfaces
// because interfaces don't have a index signature.
// We augment the Prisma generated types to have an index signature.
// Read more https://github.com/microsoft/TypeScript/issues/15300#issuecomment-1320528385
export type RecursiveInterfaceToType<T> = {
[K in keyof T]: T[K] extends object
? RecursiveInterfaceToType<T[K]>
: _DistributiveInterfaceToType<T[K]>
}

// Applies the RecursiveInterfaceToType to all types in a union
type _DistributiveInterfaceToType<T> = T extends unknown
? RecursiveInterfaceToType<T>
: never
1 change: 0 additions & 1 deletion waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ genSdkReal :: AppSpec -> Generator [FileDraft]
genSdkReal spec =
sequence
[ genFileCopy [relfile|vite-env.d.ts|],
genFileCopy [relfile|prisma-runtime-library.d.ts|],
genFileCopy [relfile|api/index.ts|],
genFileCopy [relfile|api/events.ts|],
genFileCopy [relfile|core/storage.ts|],
Expand Down

0 comments on commit c3ca958

Please sign in to comment.