A
arktype14mo ago
OnkelTem

How to declare an optional schema?

I wonder if it's possible to define a schema that is ok if data is undefined. E.g. in:
declare function foo(a?: string)v void
declare function foo(a?: string)v void
how to declare a schema for a?
18 Replies
ssalbdivad
ssalbdivad14mo ago
Just create a union with undefined type("string|undefined")
OnkelTem
OnkelTemOP14mo ago
ok, I've prepared a specific example
OnkelTem
OnkelTemOP14mo ago
import { type } from 'arktype';

// Options
const options$ = type({
lang: 'string',
text: 'string',
});

// Config
const config$ = type({
token: 'string',
// How to make this Partial?
options: options$,
});

class Tts {
#token: string;

// This is Partial<Options>
#config: Partial<typeof options$.infer>;

constructor(config: typeof config$.infer) {
this.#token = config.token;
this.#config = config.options;
}
}
import { type } from 'arktype';

// Options
const options$ = type({
lang: 'string',
text: 'string',
});

// Config
const config$ = type({
token: 'string',
// How to make this Partial?
options: options$,
});

class Tts {
#token: string;

// This is Partial<Options>
#config: Partial<typeof options$.infer>;

constructor(config: typeof config$.infer) {
this.#token = config.token;
this.#config = config.options;
}
}
OnkelTem
OnkelTemOP14mo ago
How can I make this partial?
No description
ssalbdivad
ssalbdivad14mo ago
Sounds like something @Dimava might have built at some point. The most straightforward solution would be to write a generic function that takes a type, Object.fromEntries(Object.entries(...).map(...)) its definition so that each key gets ? appended, then return it inferred like you did Partial will be builtin as a generic in beta, but I wanted to hold off adding APIs for that stuff until generics were available since I knew that would eventually be how they worked
OnkelTem
OnkelTemOP14mo ago
Also, I wonder if I reuse type correctly? Shouldn't I instead use scope?
ssalbdivad
ssalbdivad14mo ago
What you did is good. Scopes are also good, a lot of it is preference. I would say if you aren't writing complex/cyclic expressions, just using types is usually enough
OnkelTem
OnkelTemOP14mo ago
Yeah So how to achieve this Partial thing now? A workaround would be... ?
ssalbdivad
ssalbdivad14mo ago
I wrote it!
The most straightforward solution would be to write a generic function that takes a type, Object.fromEntries(Object.entries(...).map(...)) its definition so that each key gets ? appended, then return it inferred like you did
OnkelTem
OnkelTemOP14mo ago
Erm...
ssalbdivad
ssalbdivad14mo ago
FINE
OnkelTem
OnkelTemOP14mo ago
I don't get it sorry
ssalbdivad
ssalbdivad14mo ago
Something like this:
const partial = <t extends object>(base: Type<t>) =>
type(
Object.fromEntries(
Object.entries(base.definition as object).map(([k, v]) => [`${k}?`, v])
) as Infer<Partial<t>>
)
const partial = <t extends object>(base: Type<t>) =>
type(
Object.fromEntries(
Object.entries(base.definition as object).map(([k, v]) => [`${k}?`, v])
) as Infer<Partial<t>>
)
OnkelTem
OnkelTemOP14mo ago
I think I start getting the principle of making these hacks
ssalbdivad
ssalbdivad14mo ago
Hopefully you'll have it totally down right before beta then you won't need it anymore :p
OnkelTem
OnkelTemOP14mo ago
then partial will be a built-in utility, right? 1274 commits ahead now... my gosh
ssalbdivad
ssalbdivad14mo ago
Right You can use it like Partial<user> in a string or you'll be able to do partial(user) and pass a type in
Want results from more Discord servers?
Add your server