Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing generic functions (like a transformer) correctly #2075

Closed
bjesuiter opened this issue Feb 21, 2023 · 1 comment
Closed

Typing generic functions (like a transformer) correctly #2075

bjesuiter opened this issue Feb 21, 2023 · 1 comment

Comments

@bjesuiter
Copy link

bjesuiter commented Feb 21, 2023

Given the following function type:
ChunkTransformFunc<P, R> = (chunk: P) => R

I want to be able to express:

  1. The Param should be of type P
  2. The Return Type should be of type R (normally unrelated to P)

ChatGPT Gives me something like this:

const ChunkTransformFuncSchema = z
  .function(z.zodTuple([z.unknown() as any]), z.unknown())
  .args(z.zodTuple([z.unknown() as any] as const))
  .transform((func) => {
    const [inputSchema, outputSchema] = z.getParsedType(func);
    return z.function(inputSchema[0], outputSchema);
  });

But this has some problems:

  1. z.zodTuple does not exist => rewriting to z.tuple()
  2. Then this line does not work anymore, due to readonly something somewhere: .args(z.tuple([z.unknown() as any] as const));

I would like to have something like a z.generic() function which allows a generic to be defined as input and to be retrieved as an output type for example.
Maybe like this:

z
.function(z.generic('P'), z.generic('R'))
.args(z.generic('P))
.returns(z.generic('R'))
@colinhacks
Copy link
Owner

This is known as a "higher-kinded type" and it isn't representable in TypeScript: microsoft/TypeScript#1213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants