colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

parsed data is becoming never #3743

Open AmruthjithSunil opened 2 weeks ago

AmruthjithSunil commented 2 weeks ago

image(1) const parsedData = getSuggestionsSchema.safeParse( action.payload?.data, ); if (!parsedData.data) { console.error("getSuggestions", parsedData.error); state.suggestions = initialState.suggestions; state.userMessage = initialState.userMessage; return; } const parsedError = parsedData.error; parsedError && console.warn(parsedError); parsedData.error && console.error(parsedData.error);

only in the last line inside the console error. Im getting error "Property 'error' does not exist on type 'never'"

sunnylost commented 2 weeks ago

Before the if(!parsedData.data) statement, the type of parsedData can either be SafeParseSuccess or SafeParseError. However, after the if statement, it is resolved to SafeParseSuccess, which means its error property is undefined. Therefore, in parsedData.error && console.error(parsedData.error), it's console will not execute because the precondition is false.

AmruthjithSunil commented 2 weeks ago

Thanks for responding. image But the type of parsed data is becoming never not SafeParseSuccess and why "const parsedError = parsedData.error; " line is not showing error.

sunnylost commented 2 weeks ago

You should hover over parsedData.error && ....

https://www.typescriptlang.org/docs/handbook/2/narrowing.html#truthiness-narrowing

AmruthjithSunil commented 2 weeks ago

My doubt is if optional key part of schema failed at that case there would be error value right. even though parsedData.data exist.

sunnylost commented 2 weeks ago

Which part do you mean?

AmruthjithSunil commented 2 weeks ago

Imagine if the schema had 3 keys. 2 required and 1 optional key. If optional key part of schema failed, I'll still get parsedData.data containing two required keys right? And I'll also get parsedData.error right? since optional key part failed.

sunnylost commented 2 weeks ago

After digging a bit deeper, I found it's more complicated than I initially thought.

I've simplified some code to explain, and I hope this will be helpful.

https://www.typescriptlang.org/play/?#code/C4TwDgpgBA8g0gHgCoD4oF4oG8BQV8AmAhsEQFxRJ74QBOtA9rQPwUB2EAbnTgL445QkKADEiASwA2yNJlz4oxUqygdutalDqNaFKv0HhoAJQgBnAK6TgyADSjZsRKigAfUROkiUAgGYW2AGNgcQY2KAAPAAoASmxNAHoEqAABYDMAWnEAczYmCE1AsLNgKF1TS2sEACMGBkkIIjZ7AFF6JkcsAwVxXyjaADolIjj5BXxaCGALWjZNbomoJNUGUrYidoB3cTZsgR6+gEJB4dGl5OGoIqsCKGroXyJJMwh7MwYyq4Ybu+h4GrqDSaPnGi2WJSkkhWaw2jG2u00Ckm01m832YOS6y2O2yUGAH3+tXqjTYKHs1QspQAkoowgByUrAAAWOwA1niWWYoOIuUV6BBggNEeduaUmUQufjfmUIJISBBbttmVAAOIQDi0cSBLlNW7pOm8sLARhQ3ySBibKBNJ4gMw8zSDbRMKAAMhdXzY7waA3N2X6AydtBi6O5fUd7SD8VBn2WWLhOLxHzUPGj4Z0aN4QA

I suggest checking parsedData.success instead of parsedData.data. Since parsedData is a discriminated union, using success will provide a more accurate type check.

AmruthjithSunil commented 2 weeks ago

Thank you. My goal is to do console.warn if only optional part of schema fails and do console.error if required part fail. What is the recommended way to do this?

sunnylost commented 2 weeks ago

You can break the whole schema into requiredSchema and optionalSchema, no need to combine them togather.

AmruthjithSunil commented 1 week ago

Thank you. I'll do that.