sanctuary-js / sanctuary-def

Run-time type system for JavaScript
MIT License
294 stars 23 forks source link

`this` gets lost for Type.validate #220

Closed dakom closed 5 years ago

dakom commented 5 years ago

The following causes an exception to be thrown:

const validate = type => type.validate
validate ($.String) ("foo");

However, if we bind $.String.validate to give it the right this, it works:

const validate = type => type.validate;
$.String.validate = $.String.validate.bind($.String);
validate ($.String) ("foo");
davidchambers commented 5 years ago

validate is currently a “private” method; I haven't given any thought to its ergonomics in contexts other than the sanctuary-def source code. I'm in favour of adding $.validate (as proposed in #219), which will obviate the need for usability improvements to this private method. :)