sindresorhus/ow

Add `optional` predicate #58

SamVerschueren posted onGitHub

This is something that popped in my head while I was exploring the any and optional predicate. Currently for any we have the following syntax.

ow(5, ow.any(ow.string, ow.number));

The proposed syntax for optional is

ow(x, ow.optional.string);

I already spent quite some time trying to get the type detection work correctly, but I just couldn't get it working. An alternative in line with the any predicate could be ow(x, ow.optional(ow.string)), but that's much ow's there.

An alternative syntax for the previous examples could be

ow.any(5, ow.string, ow.number)
ow.optional(x, ow.string)

The type definition for optional is now very easy to do because x should be either the type of the predicate or undefined. Using a chained operator like ow.optional.string on the other hand is very hard to do (might be even impossible).

I also think it would be easier to add more main operators like not. For instance ow.not(5, ow.string). The only downside is that I think ow(5, ow.not.string) is more readable. Same goes for ow(x, ow.optional.string).

I just wanted to discuss this before we decide to release and make the project opensource. Because it's quite breaking. I would be fine with both approaches. There are benefits and downsides to both. There might be even an other way of dealing with this that I didn't think of so just go ahead and let me know what you guys think.

// @sindresorhus @kevva @vadimdemedes


Fund this Issue

$80.00
Rewarded

Rewarded pull request

Recent activities

samverschueren was rewarded by sindresorhus for sindresorhus/ow# 58
about 6 years ago
samverschueren submitted an output to  sindresorhus/ ow# 58
about 6 years ago