Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Untyped command-zod output #194

@Xample

Description

@Xample

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",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions