From def71286059f44190eba239b151c860c4effbe34 Mon Sep 17 00:00:00 2001 From: unnoq Date: Mon, 31 Mar 2025 15:50:38 +0700 Subject: [PATCH] test: improve reverse infer tests --- packages/client/src/types.test-d.ts | 16 +++++++++------- packages/server/src/procedure-action.test-d.ts | 15 +++++++++------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/client/src/types.test-d.ts b/packages/client/src/types.test-d.ts index 1a80c8b50..0a8e80b62 100644 --- a/packages/client/src/types.test-d.ts +++ b/packages/client/src/types.test-d.ts @@ -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() + expectTypeOf(input).toEqualTypeOf() 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>() + expectTypeOf(client).toMatchTypeOf<(input: string, options: { context?: ClientContext, signal?: AbortSignal }) => Promise>() }) it('infer correct input', () => { client('123') + // @ts-expect-error - invalid input client(undefined) + // @ts-expect-error - missing input client() // @ts-expect-error - invalid input @@ -25,11 +27,11 @@ describe('client', () => { client({}) }) - it('require non-undefindable input', () => { - const client = {} as Client + it('optional undefinedable input', () => { + const client = {} as Client client({ val: '123' }) - // @ts-expect-error - missing input + client(undefined) client() // @ts-expect-error - invalid input client({ val: 123 }) @@ -79,6 +81,6 @@ describe('client', () => { it('can reverse infer', () => { expectTypeOf< typeof client extends Client ? [C, I, O, E] : never - >().toEqualTypeOf<[{ cache?: boolean }, string | undefined, number, Error | ORPCError<'OVERRIDE', unknown>]>() + >().toEqualTypeOf<[{ cache?: boolean }, string, number, Error | ORPCError<'OVERRIDE', unknown>]>() }) }) diff --git a/packages/server/src/procedure-action.test-d.ts b/packages/server/src/procedure-action.test-d.ts index 8da77bcf1..496c90a19 100644 --- a/packages/server/src/procedure-action.test-d.ts +++ b/packages/server/src/procedure-action.test-d.ts @@ -25,11 +25,13 @@ it('UnactionableError', () => { }) describe('ActionableClient', () => { - const action = {} as ActionableClient<{ input: string } | undefined, { output: number } | undefined, ActionableError | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>> + const action = {} as ActionableClient<{ input: string }, { output: number }, ActionableError | 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 @@ -38,12 +40,13 @@ describe('ActionableClient', () => { await action('invalid') }) - it('require non-undefindable input', async () => { - const action = {} as ActionableClient<{ input: string }, { output: number }, ActionableError | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>> + it('optional undefindable input', async () => { + const action = {} as ActionableClient<{ input: string } | undefined, { output: number }, ActionableError | 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 @@ -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) { @@ -76,7 +79,7 @@ describe('ActionableClient', () => { it('can reverse infer', async () => { expectTypeOf< typeof action extends ActionableClient ? [I, O, E] : never - >().toEqualTypeOf<[{ input: string } | undefined, { output: number } | undefined, ActionableError | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>]>() + >().toEqualTypeOf<[{ input: string }, { output: number }, ActionableError | ORPCError<'INTERNAL_SERVER_ERROR', { time: number }>>]>() }) })