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

Allow potentially undefined functions to be checked #184

Open
wants to merge 3 commits into
base: master
from

Conversation

@TheGuardianWolf
Copy link

TheGuardianWolf commented Aug 23, 2020

proposed changes to fix #147


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


@sindresorhus
Copy link
Owner

sindresorhus commented Aug 23, 2020

Can you add some tests?

@TheGuardianWolf
Copy link
Author

TheGuardianWolf commented Aug 23, 2020

Sure, I will do that

@TheGuardianWolf TheGuardianWolf marked this pull request as draft Aug 24, 2020
@TheGuardianWolf TheGuardianWolf force-pushed the TheGuardianWolf:patch-1 branch from 80cb6a4 to c24a8f5 Aug 24, 2020
@TheGuardianWolf
Copy link
Author

TheGuardianWolf commented Aug 24, 2020

I've put a few tests for the optional function check

ow((() => {}) as any as ((() => void) | undefined), ow.optional.function);

This case originally triggered a TS error, but doesn't on my branch, do you have any additional tests in mind?

@TheGuardianWolf TheGuardianWolf marked this pull request as ready for review Aug 24, 2020
for (const {validator, message} of this.context.validators) {
if (this.options.optional === true && value === undefined) {
continue;
}

const result = validator(value);
const knownValue = value as T;

This comment has been minimized.

@sindresorhus

sindresorhus Sep 3, 2020

Owner

Do you really have to use as casting here? TS should in theory be able to infer it correctly as we check for undefined on line 91.

This comment has been minimized.

@TheGuardianWolf

TheGuardianWolf Sep 6, 2020

Author

That casting is required since without a conditional, I don't think TS will infer the type. I think if we enclose the following code in value !== undefined and use else { continue }, type inference will work the way you expect.

I'll give you a few options soon when I test a few conditionals

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

Successfully merging this pull request may close these issues.

2 participants
You can’t perform that action at this time.