castore icon indicating copy to clipboard operation
castore copied to clipboard

Untyped command-zod output

Open Xample opened this issue 11 months ago • 0 comments

Describe the bug If you provide a schema for the command-zod output, it won't type the actual returned by the handler's value.

This is because the OUTPUT type is inferred from the passed into parameter handler's callback.

This problem is here

And to prevent typescript to do so, we should use the NoInfer utility type (starting ts 5.4)

Here is a fix for the line

        handler: (input: INPUT, eventStores: $EVENT_STORES, ...context: CONTEXT) => Promise<NoInfer<OUTPUT>>;

To Reproduce Here is a broken snippet (string input / string output) but provide (string input/ number output) handler

import z from 'zod';
import { ZodCommand } from '@castore/command-zod';

const input = z.string();
const output = z.string();

new ZodCommand({
  commandId: 'PLAY_DUMB',
  requiredEventStores: tuple(),
  inputSchema: input,
  outputSchema: output,
  handler: async (commandInput, []) => {
    return 42;
  },
});

Without the NoInfer, no error is caught:

Image

Expected behavior + Screenshots

While everything works as expected using the NoInfer type utility:

Image

Note that, if no output schema is passed, the output will probably be any (as it won't be inferred from the returned handler's value)

Additional context "@castore/command-zod": "^2.4.1", "@castore/core": "^2.4.1",

Xample avatar Mar 04 '25 19:03 Xample