sanctuary-js / sanctuary-def

Run-time type system for JavaScript
MIT License
293 stars 24 forks source link

types: define NonEmpty type constructor #145

Closed davidchambers closed 7 years ago

davidchambers commented 7 years ago

I'll explain how I arrived at this pull request.

  1. I wrote something like this, forgetting to specify the types:

    const f = def('f', {}, [], _f);
  2. I ran the code and got an unhelpful error message:

    RangeError: Invalid array length
  3. I set out to add a length check and a custom error message for an empty type array.

  4. I remembered that we have an elegant way to avoid ad hoc type checking. :D

  5. I defined NonEmptyArray for internal use to improve the error message.

  6. I decided to export NonEmptyArray (and NonEmptyString, for symmetry).

safareli commented 7 years ago

I think you can define one NonEmpty(Type) for any Type which is Eq and Monoid eq(a, empty(a.constructor))

davidchambers commented 7 years ago

Wonderful suggestion, Irakli! Please have a look at the updated patch. :)