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

Incorrect inferred type of unknown keys policy after object merge #1300

Closed
implicitdef opened this issue Aug 1, 2022 · 0 comments
Closed

Comments

@implicitdef
Copy link

Hi, I found something. When merging two schema of objects with a different policy for unknown keys (one "strict" and the other "strip"), the inferred type of the resulting schema is inconsistent with the runtime behavior.

// This one is "strict"
const schemaA = z
    .object({
        a: z.string(),
    })
    .strict()

// This one is "strip"
const schemaB = z
    .object({
        b: z.string(),
    })

// Merge them
// According to the docs, the policy from B overrides the one from A, so it should be a "strip"
const mergedSchema = schemaA.merge(schemaB)

 // This doesn't throw an error : the schema is indeed "strip'
mergedSchema.parse({
    a: 'xxxxx',
    b: 'xxxxxx',
    other: 'xxxxx',
})

// However the inferred type of the merge schema uses "strict"
// This compiles, it should not
const strictSchema: z.ZodObject<any, 'strict'> = mergedSchema
// This doesn't compile, it should
const stripSchema: z.ZodObject<any, 'strip'> = mergedSchema
colinhacks pushed a commit that referenced this issue Sep 6, 2022
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

1 participant