You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Loving this library so far. I find it significantly more expressive and easier to use than io-types which I've used previously. I particularly like that there is an enforcement mechanism for functions right out of the box in Contract.
Unfortunately, I find Contract excessively strict in that it must enforce return types. The stated mission of runtypes is to bring untyped data "into the fold". In my use cases, it generally holds that once data has been brought "into the fold", passing it through a function doesn't take it "out of the fold", so return type checking is usually not necessary. Plus the return type may not be easy or useful to represent as a runtype type.
I've found a simple workaround for this:
type Skip<T> = {
'_falseWitness': T,
assert: any,
check: any,
validate: any,
guard: any,
reflect: any
}
// return type: 3
const x = Contract(Record({}), Unknown as Skip<3>).enforce(arg => 3)
But it might be nice to have a function called e.g. InputContract or ArgContract that just echoes the return type.
The text was updated successfully, but these errors were encountered:
There's more proper way around, you can use Guard runtype like this:
constx=Contract(Record({}),Guard((x): x is 3=>true),).enforce(arg=>3);
But I understand this is still verbose. I'd rather add methods such as enforceArguments and enforceResult than adding new runtypes; these methods just ignore the return runtype or the parameters runtypes respectively.
Hi,
Loving this library so far. I find it significantly more expressive and easier to use than io-types which I've used previously. I particularly like that there is an enforcement mechanism for functions right out of the box in
Contract
.Unfortunately, I find Contract excessively strict in that it must enforce return types. The stated mission of runtypes is to bring untyped data "into the fold". In my use cases, it generally holds that once data has been brought "into the fold", passing it through a function doesn't take it "out of the fold", so return type checking is usually not necessary. Plus the return type may not be easy or useful to represent as a runtype type.
I've found a simple workaround for this:
But it might be nice to have a function called e.g.
InputContract
orArgContract
that just echoes the return type.The text was updated successfully, but these errors were encountered: