Skip to content

Commit

Permalink
fix typing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Jan 2, 2022
1 parent fdf6287 commit e05ff99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export class JsonRpcClient<Api extends JsonRpcApi = EmptyObject> {
configOverrides?: Partial<JsonRpcCreateConfig>
): Promise<Either<JsonRpcError, GetResponseFromContract<Api, M>>>;

async exec<M extends keyof Api = any>(
method: GetParamsFromContract<Api, M> extends undefined ? M : never,
params?: undefined,
id?: string,
configOverrides?: Partial<JsonRpcCreateConfig>
): Promise<Either<JsonRpcError, GetResponseFromContract<Api, M>>>;

async exec<Response>(
method: Api extends EmptyObject ? string : never, // maybe add a branded type??
params?: JsonRpcParams,
Expand All @@ -147,7 +154,7 @@ export class JsonRpcClient<Api extends JsonRpcApi = EmptyObject> {
*/
async exec(
method: string,
params: JsonRpcParams,
params?: JsonRpcParams,
id?: string,
configOverrides?: Partial<JsonRpcCreateConfig>
) {
Expand Down
6 changes: 5 additions & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe("contracts", () => {
name: string;
};
type MyApiContract = {
GetThingWithNoParams: () => GetFooResponseDto;
getFoo: (params: GetFooRequestDto) => GetFooResponseDto;
getBar: (params: { age: number }) => { bar: string };
};
Expand All @@ -38,9 +39,12 @@ describe("contracts", () => {
expect(response.result.name).toBe(fixtures.withSuccess.payload.name);
}

// should be no error
newClient.exec("GetThingWithNoParams"); // should not have params

/** dtslint example */
// @ts-expect-error
newClient.exec("getFoo"); // no params
newClient.exec("getFoo"); // no params -- should have params

// @ts-expect-error
newClient.exec("getBar", { fooId: 123 }); // wrong params
Expand Down

0 comments on commit e05ff99

Please sign in to comment.