Skip to content
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

Contract: skip return type checking #257

Open
MattiasMartens opened this issue May 10, 2021 · 1 comment
Open

Contract: skip return type checking #257

MattiasMartens opened this issue May 10, 2021 · 1 comment

Comments

@MattiasMartens
Copy link

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:

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.

@yuhr
Copy link
Collaborator

yuhr commented May 13, 2021

There's more proper way around, you can use Guard runtype like this:

const x = 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.

@yuhr yuhr added this to In progress in Development Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development
In progress
Development

No branches or pull requests

2 participants