ow icon indicating copy to clipboard operation
ow copied to clipboard

`Ow` instances are mutable

Open mmkal opened this issue 4 years ago • 1 comments

This can lead to some pretty strange behaviour:

const ow = require('ow')

const array = ow.array

ow([1], array) // ok
ow([''], array.ofType(ow.string)) // ok
ow([1], array) // ArgumentError: (array) Expected `item` to be of type `string` but received type `number`

Many users won't hit this because ow.array and all the other props return a new value every time they're accessed. This should probably be documented at a minimum, but I wondered if there'd be any interest in a refactor to make the Predicates and their validators immutable, and replace addValidator with withValidator or similar which would return a new instance.

mmkal avatar Dec 10 '20 02:12 mmkal

I'm totally open to making it immutable.

sindresorhus avatar Dec 19 '20 17:12 sindresorhus