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:
how to declare a schema for
a
?18 Replies
Just create a union with undefined
type("string|undefined")
ok, I've prepared a specific example
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
How can I make this partial?
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 workedAlso, I wonder if I reuse type correctly? Shouldn't I instead use scope?
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
Yeah
So how to achieve this Partial thing now? A workaround would be... ?
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
Erm...
FINE
I don't get it sorry
Something like this:
I think I start getting the principle of making these hacks
Hopefully you'll have it totally down right before beta then you won't need it anymore :p
then
partial
will be a built-in utility, right?
1274 commits ahead now... my goshRight
You can use it like
Partial<user>
in a string or you'll be able to do partial(user)
and pass a type in