-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Property does not exist on type '{ [k in keyof addQuestionMarks]...' #995
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
Comments
Found a solution here. Is this intended behaviour or a bug? |
I agree that this looks like a minor type bug. Personally, rather than changing the runtime behavior, I’d suggest overriding the types: import { z } from "zod";
export const createSchema = <T extends unknown>(schema: z.ZodSchema<T>) => {
return z.object({
foo: schema,
bar: z.string()
});
};
export const createHandler = <T extends unknown>(
schema: z.ZodSchema<T>,
data: unknown
): void => {
const eventSchema = createSchema(schema)
const result = eventSchema.parse({foo: data, bar: 'baz'})
console.log((result as unknown as {foo: T}).foo) // Error!
console.log(result.bar) // No error
};
createHandler(z.string(), 'hello');
createHandler(z.number(), 123); |
Hi, I just wanted to add this small reproduction of the issue I made, I'm working around it right now: https://codesandbox.io/s/angry-mcclintock-3wzhsf?file=/src/index.ts |
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. |
How can we remove the 'wontfix' label? It looks like a real bug. I end up working around it with code like this: /**
* While Zod explicitly documents the way to create a schema from a generic
* function, it only works when the function is called with non-generic type
* arguments. If it is called with generic types from a generic function,
* properties using generic types may be wrongly removed from the output type,
* causing TypeScript to report errors when accessing these properties.
*
* https://github.com/colinhacks/zod/issues/995
*
* This function provides a type-unsafe way to work around the issue. It uses
* type assertion to silence the error when accessing data[key] and returns the
* value with the type inferred from the schema. Since it doesn't check the data
* object against the provided schema, this operation can be unsafe.
*
* Don't use the function if you can access the object normally.
*
* @param schema - Zod schema
* @param data - Data object validated with the Zod schema
* @param key - Property key to get the value
* @returns - Property value
*/
function zodWorkAround<
ZR extends zod.ZodRawShape,
ZU extends 'passthrough' | 'strict' | 'strip',
ZC extends zod.ZodTypeAny,
Data extends Record<string, unknown>,
Schema extends zod.ZodObject<ZR, ZU, ZC, Data, Data>,
Key extends keyof Schema['shape'],
>(
schema: Schema, data: Data, key: Key,
): zod.infer<Schema['shape'][Key]> {
return (data as Record<Key, unknown>)[key];
}
const payload = schema.parse(unsafePayload);
const field = zodWorkAround(schema, payload, 'field'); |
Should we reopen the issue, or I should open a new issue? |
@lantw44 if this issue still exists, you should submit a new issue and reference this issue in the description |
Hi, I'm not sure if this is a bug or if TypeScript just needs some more guidance but I'm having an issue with a generic function for generating schemas. Specifically the return type of the .parse() method
The error that I'm getting on the
console.log(result.foo)
says:Here is a CodeSandbox with the same code as above.
https://codesandbox.io/s/nervous-banach-vkf8d5?file=/src/index.ts
The text was updated successfully, but these errors were encountered: