-
-
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
v3 function schemas don't show error for missing fields on returns #341
Comments
This is unfortunately an inevitable side-effect of how z.function().args(z.string()).implement(arg => arg.length);
// inferred type: (arg: string)=>number |
@colinhacks Can there be a |
I just played around with this ideas and I can't get it to work. Here are the implementations of implement = <F extends InnerTypeOfFunction<Args, Returns>>(func: F): F => {
const validatedFunc = this.parse(func);
return validatedFunc as any;
};
strictImplement = (
func: InnerTypeOfFunction<Args, Returns>
): InnerTypeOfFunction<Args, Returns> => {
const validatedFunc = this.parse(func);
return validatedFunc as any;
}; Even using strictImplement, your issue isn't resolved: const func = z
.function()
.args(z.string())
.returns(z.object({ name: z.string() }))
.strictImplement((val) => {
return { name: val, asdf: 1234 };
}); I'm afraid you're hitting against a fundamental property of structural type systems like TypeScript. Consider this: type MyFunc = (arg: string) => { name: string };
const myFunc: MyFunc = (arg) => {
return { name: arg, extraKey: 1234 };
}; This also compiles without error, even with the strictest compilation settings. I don't think this is possible. |
@colinhacks oh yeah, that's strange. Your example at the bottom should error, but TS allows it :S. Let me do some digging to whether there is an issue on the TS side for this. |
This does error: type CustomType = { name: string };
const myFunc = (arg): CustomType => {
return { name: arg, extraKey: 1234 };
}; Tracked here: microsoft/TypeScript#43365 |
@colinhacks reporting back here, this issue is getting some 👀 from the TS type: microsoft/TypeScript#43365. TLDR; have to explicitly set the return type for this to work ATM type CustomType = { name: string };
const myFunc = (arg): CustomType => {
return { name: arg, extraKey: 1234 };
}; A middle ground to work for this lib is to infer and set the return type for the function inside of |
Yeah not ideal. This isn't terrible though: const func = z
.function()
.args(z.string())
.returns(z.object({ name: z.string() }));
func.implement((arg): ReturnType<z.infer<typeof func>> => {
// stuff
}); |
Ah interesting. I was thinking of this: export const func = z
.function()
.args(z.string())
.returns(TestSchema)
.implement((arg1): z.infer<typeof TestSchema> => {
// write code
}) |
Yep either one is good. Note that if |
Given code:
The function returns additional fields that are not on the returns schema. The expected behavior is that TS would throw an error due to
key2
being an additional field on the type. Addingstrict()
does not fix this eitherThe text was updated successfully, but these errors were encountered: