Type.any but with specific scope?

Is there a way to use type.any but with the scope bound to some type? My use case is that I have a special scope that does some magic for internationalization and I need a way to wrap (or just refer to) types externally (like on the website example) but with the scope bound to my special Scope
12 Replies
TizzySaurus
TizzySaurus2w ago
Type<unknown, MyScope>
Tehrin
TehrinOP2w ago
But then I lose the type safety of knowing T right? (And “infer” and “inferIn”) fields?
TizzySaurus
TizzySaurus2w ago
Well type.Any doesn't know the T either... that's the point of it
Tehrin
TehrinOP2w ago
You can have a function foo<T>(x: type.Any<T>)
TizzySaurus
TizzySaurus2w ago
Right... So do foo<T>(x: Type<T, MyScope>) then?
Tehrin
TehrinOP2w ago
Hmm, right, it does, but then what is Any for? In my particular use case Type<T, MyScope> works fine (inference and all), for most types but not for piped types (Type<(In: {})> => Out) and I thought Any would fix it, but apparently that's an issue either with Any as well, sorry for starting this thread Any just for anyone stumbling on the same issue, inference is somewhat broken with this, but overloading your function will fix it (snippet from my code):
export function validator<I, O>(schema: Type<(In: I) => O, Forms>): Adapter<(In: I) => O>;
export function validator<T>(schema: Type<T, Forms>): Adapter<T> {
export function validator<I, O>(schema: Type<(In: I) => O, Forms>): Adapter<(In: I) => O>;
export function validator<T>(schema: Type<T, Forms>): Adapter<T> {
TizzySaurus
TizzySaurus2w ago
Yeah, you shouldn't use T in the return for this very reason What you likely want is
<T extends Type<unknown, MyScope>(schema: T): Adapter<T["infer"]> {
<T extends Type<unknown, MyScope>(schema: T): Adapter<T["infer"]> {
Because T["Infer"] will handle pipes, defaults, etc.
Tehrin
TehrinOP2w ago
Adapter<T> is one of my wrappers (it wraps superforms validator, and it does use T["infer"]), but thanks 🙂 Altough, the T extends Type<unknown, MyScope> is much better with inference than Type<T, MyScope>, that's really helpful, thanks 🙂
TizzySaurus
TizzySaurus2w ago
It's impossible for it to use T["infer"] because your T isn't the ArkType type lol You need T to be the entire ArkType type, not just the "runtime" type (i.e. Type<number> and not number), I think I see what you mean now actually
Tehrin
TehrinOP2w ago
Inside I was doing Adapter<T> = SomeWrapper<Type<T, MyScope>["infer"]>
TizzySaurus
TizzySaurus2w ago
Yeah, it should still be the entire type
Tehrin
TehrinOP2w ago
The extends Type<unknown, MyScope> is still better, I just need to get used to variance rules around unknown

Did you find this page helpful?