Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ow.create does not return an Assertion Functions type / asserts annotation is missing #210

Open
sounisi5011 opened this issue May 28, 2021 · 1 comment

Comments

@sounisi5011
Copy link

sounisi5011 commented May 28, 2021

Currently, ow() funciton is of type Assertion Functions.

ow/source/index.ts

Lines 31 to 46 in f26115d

/**
Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
@param value - Value to test.
@param predicate - Predicate to test against.
*/
<T>(value: unknown, predicate: BasePredicate<T>): asserts value is T;
/**
Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails.
@param value - Value to test.
@param label - Label which should be used in error messages.
@param predicate - Predicate to test against.
*/
<T>(value: unknown, label: string, predicate: BasePredicate<T>): asserts value is T;

However, the ReusableValidator returned by ow.create() is not an Assertion Functions type.
ReusableValidator returns a void type!
In addition, the value argument does not seem to be of type unknown...

ow/source/index.ts

Lines 53 to 60 in f26115d

/**
Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
@param value - Value to test.
@param label - Override the label which should be used in error messages.
*/
// eslint-disable-next-line @typescript-eslint/prefer-function-type
(value: T, label?: string): void;

For this reason, using ow.create() in TypeScript does not provide proper type validation.

@cpiber
Copy link
Contributor

cpiber commented Sep 24, 2021

I have added something to help with this in #224. The problem why we can't simply write asserts value is T instead of void here is the same why the following exists:

ow/source/index.ts

Lines 122 to 125 in 3384c4e

// Can't use `export default predicates(modifiers(ow)) as Ow` because the variable needs a type annotation to avoid a compiler error when used:
// Assertions require every name in the call target to be declared with an explicit type annotation.ts(2775)
// See https://github.com/microsoft/TypeScript/issues/36931 for more details.
const _ow: Ow = predicates(modifiers(ow)) as Ow;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants