4 Replies
Gabriel Silva
Gabriel SilvaOP5w ago
even if i use type("string | null").to("string") In still infers as teamId: string
ssalbdivad
ssalbdivad4w ago
Narrow is for checking data and making assertions to determine whether its valid, but not changing that data. It can't affect the input/output of a type. Morphs are for actually transforming data. Something like type("string | null").pipe(data => data ?? "default") would result in the type you expect. It seems like in the example you wrote up you are already essentially enforcing that the data must not be null, so the type really is just string in that case.
Gabriel Silva
Gabriel SilvaOP4w ago
perhaps if i explain my use case... Im trying to declare a Schema to use with RHF... Where im fine with the input type being nullable, but i would like the output to be nonNullable. I used to be able to do that with zod/v3 like this:
z.string().nullable().refine(R.isString, "Must be a string")
z.string().nullable().refine(R.isString, "Must be a string")
v4, i believe, that this does the trick:
z.string().nullable().pipe(z.string("Must be a string"))
z.string().nullable().pipe(z.string("Must be a string"))
nvm, ill address this at the type level as i should've done at the beggining
ssalbdivad
ssalbdivad4w ago
.pipe is an API you can use in ArkType as well that will allow you to change the output type, but if you're only performing validation like this and there's no transformation involved, the input and output types should be the same. I believe that should be true for Zod as well (or if its not, it's likely just inaccurate in Zod) https://arktype.io/docs/expressions#morph

Did you find this page helpful?