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

feat(testing/asserts): Use assertion signature for "assertExists" #969

Merged
merged 4 commits into from
Jun 24, 2021
Merged

feat(testing/asserts): Use assertion signature for "assertExists" #969

merged 4 commits into from
Jun 24, 2021

Conversation

jsejcksn
Copy link
Contributor

This is an improvement which allows using the function as a type guard in addition to its runtime safety. See the following TS playground link for a comparison of before/after: TS Playground

The contributing guidelines state to make sure that changes are covered by a test. However, I don't see any testing framework here for types. If I modify an existing test to include a case which requires calling the function to narrow a type before use, then any type-incompatible changes to the function later would later cause a compilation error of the test, rather than a runtime error during testing... but I'm not sure that kind of meta behavior is what the guidelines mean.

@CLAassistant
Copy link

CLAassistant commented Jun 13, 2021

CLA assistant check
All committers have signed the CLA.

@kt3k
Copy link
Member

kt3k commented Jun 14, 2021

Thank you for your suggestion! I'm in favor of this change.

As regards to testing, how about adding the lines from your example in TS playground as a test case?

const value = new URLSearchParams({value: 'test'}).get('value');
assertExists(value);
console.log(value.length);

I think this test case proves that the type guard & type predicate work.

Per Yoshiya's suggestion:
#969 (comment)
@kt3k
Copy link
Member

kt3k commented Jun 15, 2021

The change looks almost good to me, but I'm not 100% sure this doesn't break any user code. Is it possible that typescript throws errors like Can not infer T in assertExists<T>? cc @kitsonk

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks! @jsejcksn

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

Successfully merging this pull request may close these issues.

3 participants