-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Can't generically compose + transform schema without type error #2146
Comments
Bug confirmed function foo<Schema extends z.AnyZodObject> ( schema: Schema ) {
return z.object( { bar: schema } ).transform( x => x.bar )
// ^^^
// Property 'bar' does not exist on type
// '{ [k in keyof baseObjectOutputType<{ bar: Schema; }>]: baseObjectOutputType<{ bar: Schema; }>[k]; }'.
} |
This should be improved in |
@colinhacks all working with the latest release! You're an absolute legend getting that out so fast! Is there by any chance a way to avoid having to assert that the generic schema is non-null? zod/src/__tests__/generics.test.ts Line 17 in f9895ab
I feel like I actually want to type Edit: |
Still a problem in import { z } from 'npm:zod@canary'
function foo<Schema extends z.AnyZodObject> ( schema: Schema ) {
return z.object( { bar: schema } ).transform( x => x.bar )
// ^^^
// Property 'bar' does not exist on type
// '{ [k in keyof baseObjectOutputType<{ bar: Schema; }>]: baseObjectOutputType<{ bar: Schema; }>[k]; }'.
} |
@JacobWeisenburger canary releases don't seem to be triggering, I think this should fix it? #2148 |
Im experiencing a very similar issue with the function parseNameAndUpdateNameError(name: string): string {
const nameSchema = z.string().min(1).max(10).trim()
const result = nameSchema.safeParse(name);
if(!result.success){
var nameErrorMessage = result.error.issues[0].message;
// update logic
}
return result.data;
// ^^^^
}
/**
* Error:
* Property 'data' does not exist on type 'SafeParseReturnType<string, string>'.
* Property 'data' does not exist on type 'SafeParseError<string>'.ts(2339)
*/ I have tested on |
@daguitosama this is an unrelated issue. you need to add function parseNameAndUpdateNameError ( name: string ): string {
const nameSchema = z.string().min( 1 ).max( 10 ).trim()
const result = nameSchema.safeParse( name )
if ( !result.success ) {
var nameErrorMessage = result.error.issues[ 0 ].message
} else {
return result.data // no errors, yay!
}
} or return early function parseNameAndUpdateNameError ( name: string ): string {
const nameSchema = z.string().min( 1 ).max( 10 ).trim()
const result = nameSchema.safeParse( name )
if ( !result.success ) {
var nameErrorMessage = result.error.issues[ 0 ].message
return nameErrorMessage
}
return result.data // no errors, yay!
} |
O I see, thanks so much for the help, I really appreciate it ! |
If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
I wish I could man, I barely own a computer, not the best financial time for me 🥲 But, I have some ideas that might help to get more support:
If you need more ideas, will more then happy to share more on the twitters/discords... |
Thanks @johtso that fixed the CI 👍 @JacobWeisenburger that should be working now |
@colinhacks just in case it got lost amongst the other messages, any thoughts on this? #2146 (comment) I can totally just live with telling typescript that the value will never be |
Yeah, I noticed that and tried to fix it for a while. Then I realized it makes sense. Because |
@colinhacks would it not make sense to be able to have my generic type variable extend something more specific in this case? I know I'm going to be dealing with non-optional Feels like I should be able to type the argument as a non-optional object schema. And if I tried to give the function |
Say I did something like this: const nonOptionalObject = z.object({}).required()
function foo<TSchema extends typeof nonOptionalObject>(schema: TSchema, data: any) {
const zUnwrapped = z
.object({
nested: schema,
})
.transform(data => {
return data.nested
})
return zUnwrapped.parse(data) Shouldn't the types convey the fact that the schema cannot result in undefined? |
@johtso Hm that is curious. |
Say I want to do something like this:
https://tsplay.dev/mq9bjm
I get the following type error:
Is this something I should be able to do? Any pointers where I'm going wrong?
The text was updated successfully, but these errors were encountered: