change assert.ok to narrow the type #105
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This changes the return type of
assert.ok
fromvoid
toasserts actual
, making type narrowing work:This is the same as
assert.ok
in@types/node
:This is more complicated for
assert.not.ok
. The syntaxasserts !val
does not work, you have to sayasserts val is $type
. So, does a falsy union type likeasserts actual is false | '' | null | undefined | 0 | -0 | typeof NaN
work correctly for all cases? It does for my code, but I don't know if it's correct - I think it is? As a workaround, you can negate the condition and useok
to get that behavior with this PR. I'll follow up on this and some other types in an issue soon. (sorry this may be straightforward, but I believe I couldn't get this to work in a previous TS version, but it works now for the cases I have)AFAIK this change is only a win - because it only narrows the type, I don't think it can cause errors. The other changes I want to make may break existing typechecks.