Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow potentially undefined functions to be checked #184
Conversation
|
Can you add some tests? |
|
Sure, I will do that |
80cb6a4
to
c24a8f5
|
I've put a few tests for the optional function check
This case originally triggered a TS error, but doesn't on my branch, do you have any additional tests in mind? |
| 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.
This comment has been minimized.
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.
This comment has been minimized.
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
TheGuardianWolf commentedAug 23, 2020
•
edited by issuehunt-app bot
proposed changes to fix #147
IssueHunt Summary
Referenced issues
This pull request has been submitted to:
ow.optional.function