Skip to content

Commit e8cad2f

Browse files
legobeatMrtenz
andauthored
fix(types): Optional JSON params where undefined is not valid (#134)
* fix(types): Optional JSON params where undefined is not valid * Reduce type duplication by excluding `undefined` --------- Co-authored-by: Maarten Zuidhoorn <[email protected]>
1 parent 58b1611 commit e8cad2f

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/json.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
nullable,
1414
number,
1515
object,
16-
omit,
1716
optional,
1817
record,
1918
string,
@@ -178,36 +177,35 @@ export type JsonRpcError = OptionalField<
178177
>;
179178

180179
export const JsonRpcParamsStruct: Struct<Json[] | Record<string, Json>, null> =
181-
optional(union([record(string(), JsonStruct), array(JsonStruct)])) as any;
180+
union([record(string(), JsonStruct), array(JsonStruct)]);
182181

183182
export type JsonRpcParams = Json[] | Record<string, Json>;
184183

185184
export const JsonRpcRequestStruct = object({
186185
id: JsonRpcIdStruct,
187186
jsonrpc: JsonRpcVersionStruct,
188187
method: string(),
189-
params: JsonRpcParamsStruct,
188+
params: optional(JsonRpcParamsStruct),
190189
});
191190

192191
export type InferWithParams<
193192
Type extends Struct<any>,
194193
Params extends JsonRpcParams,
195-
> = Omit<Infer<Type>, 'params'> &
196-
(keyof Params extends undefined
197-
? {
198-
params?: Params;
199-
}
200-
: {
201-
params: Params;
202-
});
194+
> = Omit<Infer<Type>, 'params'> & {
195+
params?: Exclude<Params, undefined>;
196+
};
203197

204198
/**
205199
* A JSON-RPC request object.
206200
*/
207201
export type JsonRpcRequest<Params extends JsonRpcParams = JsonRpcParams> =
208202
InferWithParams<typeof JsonRpcRequestStruct, Params>;
209203

210-
export const JsonRpcNotificationStruct = omit(JsonRpcRequestStruct, ['id']);
204+
export const JsonRpcNotificationStruct = object({
205+
jsonrpc: JsonRpcVersionStruct,
206+
method: string(),
207+
params: optional(JsonRpcParamsStruct),
208+
});
211209

212210
/**
213211
* A JSON-RPC notification object.

0 commit comments

Comments
 (0)