-
|
I've got an amazing type of error message today. export const timestampToDate = (data: number): Date => {
const timestamp = new Date(data);
if (isNaN(timestamp.getTime())) {
throw new Error('Invalid timestamp');
}
return timestamp;
};
export const timestampToDateSchema = z.coerce.number().transform(timestampToDate);
export const MyUsersSchema = z.array(
z.object({
created: timestampToDateSchema,
}),
);
async do<T>(data, schema: z.ZodType<T>) {
return schema.parse(data);
}
const myUsers = MyUsersSchema.parseAsync([]);
const myUsers2 = this.do([], MyUsersSchema);As you can see, both myUsers, and myUsers2 should be typed the same. But Typescript shows an error for myUsers2 So it creates a type, which conflicts with the type itself. created has a Date type for ZodType Input, but also it has a number type for Output |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
Simple fix to avoid type error: async do<T>(data, schema: z.ZodType<T, any, any>): Promise<T> {
return schema.parse(data);
} |
Beta Was this translation helpful? Give feedback.
-
|
The code example you gave is not valid JavaScript. Please send a full reproducible code example that is valid JavaScript. |
Beta Was this translation helpful? Give feedback.
-
|
@JacobWeisenburger sure, I've created example in StackBlitz: import { z } from 'zod';
export const timestampToDate = (data: number): Date => {
const timestamp = new Date(data);
if (isNaN(timestamp.getTime())) {
throw new Error('Invalid timestamp');
}
return timestamp;
};
export const timestampToDateSchema = z.coerce
.number()
.transform(timestampToDate);
export const MyUsersSchema = z.array(
z.object({
created: timestampToDateSchema,
})
);
function bugExample<T>(data, schema: z.ZodType<T>) {
return schema.parse(data);
}
// It's ok here
const myUsers = MyUsersSchema.parseAsync([]);
// But got TS error here.
const myUsers2 = bugExample([], MyUsersSchema); |
Beta Was this translation helpful? Give feedback.
-
|
Is this what you are looking for? export const timestampToDate = ( data: number ): Date => {
const timestamp = new Date( data )
if ( isNaN( timestamp.getTime() ) ) {
throw new Error( 'Invalid timestamp' )
}
return timestamp
}
export const timestampToDateSchema = z.coerce
.number()
.transform( timestampToDate )
export const MyUsersSchema = z.array(
z.object( {
created: timestampToDateSchema,
} )
)
function bugExample<Output, Input> ( data: unknown, schema: z.ZodType<Output, z.ZodTypeDef, Input> ) {
return schema.parse( data )
}
const myUsers = MyUsersSchema.parseAsync( [] ) // It's ok here
const myUsers2 = bugExample( [], MyUsersSchema ) // fixedIf you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
@AmirL if you want to accept only one Type arg... you can do import { z } from 'zod';
export const timestampToDate = (data: number): Date => {
const timestamp = new Date(data);
if (isNaN(timestamp.getTime())) {
throw new Error('Invalid timestamp');
}
return timestamp;
};
export const timestampToDateSchema = z.coerce.number().transform(timestampToDate);
export const MyUsersSchema = z.array(
z.object({
created: timestampToDateSchema,
})
);
// function bugExample<T>(data: unknown, schema: z.ZodType<T>) {
// return schema.parse(data);
// }
function bugExample<ZT extends z.ZodType>(data: unknown, schema: ZT): Promise<ZT['_output']> {
return schema.parseAsync(data);
}
const myUsers = MyUsersSchema.parseAsync([]);
const myUsers2 = bugExample([], MyUsersSchema); |
Beta Was this translation helpful? Give feedback.


@AmirL if you want to accept only one Type arg... you can do