Closed segevfiner closed 1 year ago
you will get errors from TypeScript while trying to destructure params
Do you mean the code like this gives you an error?
serverAndClient1.addMethod("echo", ({ message }) => message);
If so, it is intentional to enforce type annotation:
interface EchoParams {
message: string
}
serverAndClient1.addMethod("echo", ({ message }: EchoParams) => message);
you will get errors from TypeScript while trying to destructure params
Do you mean the code like this gives you an error?
serverAndClient1.addMethod("echo", ({ message }) => message);
If so, it is intentional to enforce type annotation:
interface EchoParams { message: string } serverAndClient1.addMethod("echo", ({ message }: EchoParams) => message);
I was getting an error trying to annotate the parameter or destructure it, so I was actually getting an error on the second example, had to use an as
cast inside the method code and destructure there.
Can you please give me minimal reproducible example?
Try this: https://github.com/segevfiner/json-rpc-2.0-types-issue, just a pnpm build
should show the issue.
Is this the error you are getting?
src/index.ts:5:27 - error TS2345: Argument of type '({ name }: { name?: string | undefined; }) => void' is not assignable to parameter of type 'SimpleJSONRPCMethod<void>'.
Types of parameters '__0' and 'params' are incompatible.
Type 'Partial<JSONRPCParams> | undefined' is not assignable to type '{ name?: string | undefined; }'.
Type 'undefined' is not assignable to type '{ name?: string | undefined; }'.
It's strange how the same code works in this repo if I copy and paste it. I'll take a look.
Adding "strictFunctionTypes": true
to tsconfig.json
surfaces this error. In your example, "extends": "@tsconfig/node16/tsconfig.json"
inherits this setting.
Let me think of something so that it works with strict
mode! Thank you for reporting.
https://github.com/shogowada/json-rpc-2.0/blob/7bc786c03b47b1e0a93e1d8b9df2c6a2d42a4094/src/models.ts#L5
Should probably be:
Or just:
Or you will get errors from TypeScript while trying to destructure params or to type it as
object
is akin to{}
, an object with no known props.