How to cast string to number?
See original GitHub issueI have something like this:
const params = z.object({
id: z.number()
})
This fails immediately since id is a query string param and so is a string. Then I tried:
const params = z.object({
id: z.transformer(z.string(), z.number(), x => Number(x))
})
But the compiler yells:
Type 'ZodObject<{ id: ZodTransformer<ZodString, ZodNumber>; }, "passthrough", ZodTypeAny, { id: number; }, { id: string; }>' is not assignable to type 'ZodType<{ id: number; }, ZodTypeDef, { id: number; }>'.
The types of '_input.id' are incompatible between these types.
Type 'string' is not assignable to type 'number'
What am I doing wrong?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Java String to Int – How to Convert a String to an Integer
1. Use Integer.parseInt() to Convert a String to an Integer · 2. Use Integer.valueOf() to Convert a String to an Integer.
Read more >Converting strings to numbers with vanilla JavaScript
The parseInt() method converts a string into an integer (a whole number). It accepts two arguments. The first argument is the string to...
Read more >7 ways to convert a String to Number in JavaScript
7 ways to convert a String to Number in JavaScript · 1. Using parseInt() · 2. Using Number() · 3. Using Unary Operator...
Read more >How to convert a string to a number - C# Programming Guide
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long ,...
Read more >Java Convert String to int - javatpoint
We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There’s a lot to unpack here.
ZodType<Output, Def, Input>
. For transformers Input and Output are different, for all other schemas they’re the same.ZodType<P>
) then the Input defaults to Output. You can see this here:Anyway, the solution is to fully type the ZodTypes in your interface. Without your full code I can’t test it myself. This should work:
@MFogleman Hi there, I’m not using Hapi, I was just making a proof of concept. The idea was to build a very simple type-safe http server from scratch. You can check it out here. I know the code sucks but the idea was to define a route specifying the
body
,params
andreturn body
types and return the appropiate http status code if any of the validation fails. I manage to make it work by doingZodType<T, any, any>
but I’ll check out what you specified in your comment.