"Custom" type or equivalent to valibot.custom()?
I might just be missing something obvious, but I want to do something like this:
Obviously
SomeType
isn't a valid value, so I could do something like this:
But I have a feeling that's not the right way to do it. Is there something like valibot's custom?5 Replies
You can use narrow for arbitrary validation logic + if you add a type predicate like
: data is SomeType
it will be inferred as the output
So you'd start with whatever you can validate directly with other constraints, then add custom logic on top of that needed
If you can express everything you need without a custom function but just want to cast the type, you can use type(...).as<SomeType>()
Ahh okay, thanks
Sorta related, when creating a type for an object that has functions on it, I was doing this:
If I want to avoid casting I suppose I should do this?
Althought I guess that's kinda redundant because I'm assuming that's what the
Function
keyword does anywayYeah, also a type predicate is equivalent to casting so there is no difference in terms of safety
makes sense, ty
You could use
type("Function").as<MyFuncType>()
also I suppose, all roads lead to Rome haha
(with equal amounts of type-unsafety required since it's impossible to validate functions at runtime 😅)