Skip to content

Commit

Permalink
Add e2e tests for full stack type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Apr 25, 2024
1 parent c309ad3 commit 4e4cf5a
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions waspc/examples/todoApp/src/TestRpcTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
getTask,
getTasks,
createTask,
updateTaskIsDone,
deleteCompletedTasks,
toggleAllTasks,
getNumTasks,
getDate,
getAnything,
getTrueVoid,
} from 'wasp/client/operations'
import { Task } from 'wasp/entities'
import { Payload } from 'wasp/server/_types'

// For the details of this specification, see
// https://github.com/wasp-lang/wasp/pull/1090#discussion_r1159732471

// This should be [],
// but I couldn't get it to work yet.
type VoidOperationPayload = [args?: void | undefined]

// When the user doesn't specify an operation payload,
// we want to be as permissive as possible.
type UnspecifiedOperationPayload = [args?: unknown]

type TQ1 = Assert<
InputsAndOutputsAre<typeof getTask, [Pick<Task, 'id'>], Promise<Task>>
>

type TQ2 = Assert<
InputsAndOutputsAre<typeof getTasks, VoidOperationPayload, Promise<Task[]>>
>

type TQ3 = Assert<
InputsAndOutputsAre<typeof getNumTasks, VoidOperationPayload, Promise<number>>
>

type TQ4 = Assert<
InputsAndOutputsAre<typeof getDate, VoidOperationPayload, Promise<Date>>
>

type TQ5 = Assert<
InputsAndOutputsAre<
typeof getAnything,
UnspecifiedOperationPayload,
Promise<Payload>
>
>

type TQ6 = Assert<
InputsAndOutputsAre<typeof getTrueVoid, VoidOperationPayload, Promise<string>>
>

type TA1 = Assert<
InputsAndOutputsAre<
typeof createTask,
[Pick<Task, 'description'>],
Promise<Task>
>
>

type TA2 = Assert<
InputsAndOutputsAre<
typeof updateTaskIsDone,
[Pick<Task, 'id' | 'isDone'>],
Promise<Payload>
>
>

type TA3 = Assert<
InputsAndOutputsAre<
typeof deleteCompletedTasks,
UnspecifiedOperationPayload,
Promise<Payload>
>
>

type TA4 = Assert<
InputsAndOutputsAre<
typeof toggleAllTasks,
UnspecifiedOperationPayload,
Promise<Payload>
>
>

type InputsAndOutputsAre<
OperationType extends (...args: any[]) => any,
ExpectedParams,
ExpectedReturn
> = {
params: AreEqual<Parameters<OperationType>, ExpectedParams>
return: AreEqual<ReturnType<OperationType>, ExpectedReturn>
}

type Assert<T extends { params: true; return: true }> = T

type AreEqual<T, Expected> = T extends Expected
? Expected extends T
? true
: false
: false

0 comments on commit 4e4cf5a

Please sign in to comment.