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

Change in 3.21.2 breaks assignability of schema created from generics' parsed and inferred type #2196

Closed
conorbrandon opened this issue Mar 15, 2023 · 2 comments
Labels
stale No activity in last 60 days

Comments

@conorbrandon
Copy link

I have a need to create a zod object schema that only has one key-value pair. This may not be exactly the best way to do something like this (and this is a contrived example), but in version 3.21.1, it was possible to assign the parsed data to the inferred type of a schema created through generic parameters:

import { z } from 'zod';

const createSchemaAndParse = <K extends string, VS extends z.ZodString>(key: K, valueSchema: VS, data: unknown) => {
  const schema = z.object({
    [key]: vSchema
  });
  const parsed = schema.parse(data);
  const inferred: z.infer<z.ZodObject<{ [k in K]: VS }>> = parsed; // this line errors in 3.21.2
  return inferred;
};
createSchemaAndParse('foo', z.string(), { foo: '' });

Unfortunately, with 3.21.2 (and above), the above line now produces a type error.

@chrishoermann
Copy link

Maybe related to this #2184?

@stale
Copy link

stale bot commented Jun 13, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No activity in last 60 days label Jun 13, 2023
@stale stale bot closed this as completed Jul 14, 2023
colinhacks added a commit that referenced this issue Mar 22, 2024
colinhacks added a commit that referenced this issue Mar 22, 2024
* Improve addQuestionMarks, fix #2184

* Fix #2196

* Fix

* Add test case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale No activity in last 60 days
Projects
None yet
Development

No branches or pull requests

2 participants