diff --git a/src/BotApi.ts b/src/BotApi.ts index f8fc655..d30fc05 100644 --- a/src/BotApi.ts +++ b/src/BotApi.ts @@ -19,12 +19,17 @@ export declare namespace BotApi { export type Service = BotApiShape } +/** @internal */ +export type MethodArgs = void extends MethodParams[M] + ? [params?: MethodParams[M]] + : [params: MethodParams[M]] + export interface Method< M extends keyof MethodParams, E = BotApiError.BotApiError | BotApiTransport.BotApiTransportError, R = never, > { - (params: MethodParams[M]): Effect.Effect + (...args: MethodArgs): Effect.Effect } export const make: ( @@ -42,7 +47,7 @@ export const layer: Layer.Layer< export const callMethod: ( method: M, - params: MethodParams[M], + ...args: MethodArgs ) => Effect.Effect< MethodResults[M], BotApiError.BotApiError | BotApiTransport.BotApiTransportError, diff --git a/test/BotApi.test-d.ts b/test/BotApi.test-d.ts new file mode 100644 index 0000000..e96545d --- /dev/null +++ b/test/BotApi.test-d.ts @@ -0,0 +1,16 @@ +import { describe, it } from 'vitest' +import * as BotApi from '../src/BotApi.ts' + +describe('BotApi', () => { + describe('callMethod', () => { + it('should not typecheck if no params passed for methods with required params', () => { + // @ts-expect-error name is a string + BotApi.callMethod('sendMessage') + }) + it('should typecheck if no params passed for methods with all params optional', () => { + BotApi.callMethod('getMe') + BotApi.callMethod('getAvailableGifts') + BotApi.callMethod('getUpdates') + }) + }) +})