Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/client/src/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import type { ORPCError } from './error'
import type { Client, ClientContext } from './types'

describe('client', () => {
const client: Client<{ cache?: boolean }, string | undefined, number, Error | ORPCError<'OVERRIDE', unknown>> = async (...args) => {
const client: Client<{ cache?: boolean }, string, number, Error | ORPCError<'OVERRIDE', unknown>> = async (...args) => {
const [input, options] = args

expectTypeOf(input).toEqualTypeOf<string | undefined>()
expectTypeOf(input).toEqualTypeOf<string>()
expectTypeOf(options).toMatchTypeOf<{ context?: { cache?: boolean }, signal?: AbortSignal } | undefined>()
return 123
}

it('just a function', () => {
expectTypeOf(client).toMatchTypeOf<(input: string | undefined, options: { context?: ClientContext, signal?: AbortSignal }) => Promise<number>>()
expectTypeOf(client).toMatchTypeOf<(input: string, options: { context?: ClientContext, signal?: AbortSignal }) => Promise<number>>()
})

it('infer correct input', () => {
client('123')
// @ts-expect-error - invalid input
client(undefined)
// @ts-expect-error - missing input
client()

// @ts-expect-error - invalid input
Expand All @@ -25,11 +27,11 @@ describe('client', () => {
client({})
})

it('require non-undefindable input', () => {
const client = {} as Client<ClientContext, { val: string }, { val: number }, Error>
it('optional undefinedable input', () => {
const client = {} as Client<ClientContext, { val: string } | undefined, { val: number }, Error>

client({ val: '123' })
// @ts-expect-error - missing input
client(undefined)
client()
// @ts-expect-error - invalid input
client({ val: 123 })
Expand Down Expand Up @@ -79,6 +81,6 @@ describe('client', () => {
it('can reverse infer', () => {
expectTypeOf<
typeof client extends Client<infer C, infer I, infer O, infer E> ? [C, I, O, E] : never
>().toEqualTypeOf<[{ cache?: boolean }, string | undefined, number, Error | ORPCError<'OVERRIDE', unknown>]>()
>().toEqualTypeOf<[{ cache?: boolean }, string, number, Error | ORPCError<'OVERRIDE', unknown>]>()
})
})
15 changes: 9 additions & 6 deletions packages/server/src/procedure-action.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ it('UnactionableError', () => {
})

describe('ActionableClient', () => {
const action = {} as ActionableClient<{ input: string } | undefined, { output: number } | undefined, ActionableError<Error | ORPCError<'CODE', { foo: string }> | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>>
const action = {} as ActionableClient<{ input: string }, { output: number }, ActionableError<Error | ORPCError<'CODE', { foo: string }> | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>>

it('input', async () => {
await action({ input: 'input' })
// @ts-expect-error - input is invalid
await action(undefined)
// @ts-expect-error - missing input
await action()

// @ts-expect-error - not allow second argument
Expand All @@ -38,12 +40,13 @@ describe('ActionableClient', () => {
await action('invalid')
})

it('require non-undefindable input', async () => {
const action = {} as ActionableClient<{ input: string }, { output: number }, ActionableError<Error | ORPCError<'CODE', { foo: string }> | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>>
it('optional undefindable input', async () => {
const action = {} as ActionableClient<{ input: string } | undefined, { output: number }, ActionableError<Error | ORPCError<'CODE', { foo: string }> | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>>

await action({ input: 'input' })
// @ts-expect-error - missing input
await action(undefined)
await action()

// @ts-expect-error - not allow second argument
await action({ input: 'input' }, 'second' as any)
// @ts-expect-error - invalid input
Expand All @@ -54,7 +57,7 @@ describe('ActionableClient', () => {
const [error, data] = await action({ input: 'input' })

if (!error) {
expectTypeOf(data).toEqualTypeOf<{ output: number } | undefined>()
expectTypeOf(data).toEqualTypeOf<{ output: number }>()
}

if (error) {
Expand All @@ -76,7 +79,7 @@ describe('ActionableClient', () => {
it('can reverse infer', async () => {
expectTypeOf<
typeof action extends ActionableClient<infer I, infer O, infer E> ? [I, O, E] : never
>().toEqualTypeOf<[{ input: string } | undefined, { output: number } | undefined, ActionableError<Error | ORPCError<'CODE', { foo: string }> | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>]>()
>().toEqualTypeOf<[{ input: string }, { output: number }, ActionableError<Error | ORPCError<'CODE', { foo: string }> | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>]>()
})
})

Expand Down