sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.8k stars 105 forks source link

Bug: `isValid` wrong typing #222

Closed cpiber closed 2 years ago

cpiber commented 2 years ago

isValid always assumes the type of the value, because the first argument should be unknown (like it is for ow itself): https://github.com/sindresorhus/ow/blob/3728d9119a19e1a95cb58bebe602afd308c40630/source/index.ts#L39

Looking over the rest of the file, it seems this mistake was made in several places; ReusableValidator should probably also not use T for the value (and assert?)

sindresorhus commented 2 years ago

Good catch. Would you be interested in doing a pull request to fix it?

cpiber commented 2 years ago

Sure, give me a bit of time though please

cpiber commented 2 years ago

So, I just got to it and I have a problem: Ideally ReusableValidator<T> would also assert value is T, however typescript complains Assertions require every name in the call target to be declared with an explicit type annotation.. Do you happen to have a solution for that? I don't think it's elegant to have to explicitly them, and it's a breaking change...

Maybe one option would be not to include assert, but introduce a convenience type that extracts the type of ReusableValidator and provides another type that actually asserts. That way we don't have a breaking change but also a way for the user to easily get the assertion.