Hi everyone! I've been working on a cleaner replacement for toZod, and was implementing union types, and came across boolean expanding to true | false. Does anyone know of a way to prevent this behavior? Here is a playground link that demonstrates this behavior. Hover over the Test type at the very bottom to see what I mean.
#Prevent `boolean` From Expanding to `true | false` In Conditional Typing
47 messages ยท Page 1 of 1 (latest)
Also - with multiple (more than two) union types, I get nested unions. Is this possible to avoid?
this correctly extracts the type, but then it gets resolved as an object. T extends [infer U] ? z.ZodArray<ToZod<U>>
Handbook / Conditional Types
which is triggered any time you do T extends, where the left side of the extends is a "bare type parameter"
ah, i'll look at the docs. ty
[T] extends [U] is often used as a substitute for T extends U that doesn't trigger distribution
(Ah, right that is actually mentioned in the docs)
yep!
ok, one more question ๐ญ. I can't think of a way to check if an array is an array literal, because Array<T> is the same as [T, T] when using conditionals. (based on testing)
probably
type IsArray<T extends readonly unknown[]> = IsEqual<T, T[number][]>
T extends [infer First, ...infer Rest] works most of the time too
!ts
type IsArray<T> = T extends [infer First, ...infer Rest] ? [First, Rest] : never;
type A = IsArray<[...'a'[], 'b']>
// ^? - type A = never```
although it's not entirely foolproof
that said, if your tuple has a spread in it then it's probably impossible to do type manipulation on it
(well, i supposed you can use a mapped type. but what you can do with that is somewhat limited)
on the other hand it's not like zod can represent tuples with spreads anyway, right?
just tested, that also returns [First, Rest] when I provide <type>[].
I feel so bad for needing to be spoonfed, but I literally have zero idea what i'm doing when it comes to arrays and types ๐ญ
wait a minute. [T] is saying Array<T> with a size of one. That's why.
because if I input string[], it's like saying [string[]] extends [string[], ...[]]
If you want to check if an array is fixed-length the easiest thing to do is number extends array["length"] ? "not fixed" : "fixed".
I do want to say overall I think you're selling yourself short because this looks quite good to me
I don't think it's possible to dertermine between ['foo', 'bar'] and string[]
If you want to check if an array is fixed-length the easiest thing to do is number extends array["length"] ? "not fixed" : "fixed".
๐
I do want to say overall I think you're selling yourself short because this looks quite good to me
๐
That strategy can also be used in some of these other areas e.g. type IsBooleanLiteral<T> = boolean extends T ? false : true
IsUndefined should really just be T extends undefined if you're worried about any and never the best strategy is to check for those first so you can eliminate them rather then dealing with them in each case like this
which is how I learn, so maybe i'm not doing too bad after all
I just need to be able to distunguish between them for ZodUndefined, ZodAny, ZodNull, and ZodNever
or maybe i could make a try for that
Right you still can by eliminating any and never by checking for those cases first. Then you can check extends undefined normally and the only way it could be true is the literal undefined
any and never are always a thorn in your side if you have to deal with this kind of thing best to do it up front so you don't have to worry about so many edge cases ๐