A
arktype•14mo ago
OnkelTem

Need help with typing scope output properly

I've got one crazy idea to declare types as scopes to access validators independently
4 Replies
OnkelTem
OnkelTemOP•14mo ago
You can see it in action via the link However, I have one problem: a typescript error Wait, let me rewrite this example a little Ok, here is the initial idea:
OnkelTem
OnkelTemOP•14mo ago
I'd call it lazy validation You can create a class w/o any config, ok? But if you pass something to it, it will be validated Yet you're not required to do that Later on, when using this class, if at some point a property will be requested... then it WILL be checked against the initial schema and it will become required if it was required by the intial schema The foo getter does exactly this:
get foo() {
return config$$.foo.assert(this.#config.foo)
}
get foo() {
return config$$.foo.assert(this.#config.foo)
}
Note that i"m using here the original validator from the scope Now I want to automate this process 🙂 So I add a sort of an universal getter which can validate things Damn LOL, ok, nevermind. Ignore me. Hahaha I added the return type wrong
get<K extends keyof Config>(name: K): Config[K] {
return config$$[name].assert(this.#config[name]);
}
get<K extends keyof Config>(name: K): Config[K] {
return config$$[name].assert(this.#config[name]);
}
If I remove Config[K] it works by itself. So it's properly inferred But out of interest, what would be the correct return type in this case? If we try to not use functions w/o direct returns I feel like deleting this topic. And rewriting it from scratch LOL
Want results from more Discord servers?
Add your server