-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement inline optional for record #113
Conversation
99ba6c0
to
9f14cdf
Compare
I like the motivation behind this, but it changes the semantics slightly from TypeScript: { foo?: number } is not the same as { foo: number | undefined } The former says that To do this the right way, I think we would need the {
[K in keyof O][O[K] extends Optional ? +? : -?]: O[K] extends Optional<infer T> ? T : O[K]
} |
Totally agree, but I did not find other ways...
But I already do it :) The question is how the right API may look like... Current workaround by Record and Partial interaction looks weird( |
I think it's reasonable to make an alias for the /**
* Union this Runtype with another.
*/
Or<B extends Runtype>(B: B): Union2<this, B>;
/**
* Intersect this Runtype with another.
*/
And<B extends Runtype>(B: B): Intersect2<this, B>;
+ /**
+ *
+ */
+ Optional: Runtype<Static<this> | undefined>; implemented as A.Or = Or;
A.And = And;
+ A.Optional = Union(A, Literal(undefined)); |
Some languages have an algebraic datatype called I'd go for the OP's approach, which is having
Why we need it to be a discrete runtype is nothing else but the point 2. |
Anyway, the PR branch is a bit stale to continue enhancement, so I'm going to update it. |
I ended up having the shorthand syntax of |
Awesome 👍 |
Nice addition, definitely useful. When it's hitting npm? |
Sorry for being late, it should be available in |
See relevant Runtypes changes: - runtypes/runtypes#113 - runtypes/runtypes#185
}), | ||
); | ||
``` | ||
|
||
If a record has keys which _must be present_ but can be null, then use | ||
the `Record` runtype normally instead. | ||
There's a difference between `Union(String, Undefined)` and `Optional(String)` iff they are used within a `Record`; the former means "_**it must be present**, and must be `string` or `undefined`_", while the latter means "_**it can be present or missing**, but must be `string` or `undefined` if present_". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo on iff
The better way, then
Record({}).And(Partail({}))