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

ref(utils): Use type predicates in is utility functions #4124

Merged
merged 3 commits into from
Nov 5, 2021

Conversation

lobsterkatie
Copy link
Member

We have a number of is utility functions (isString, isError, and so on), and while they tell the human reading the code something about the value in question, they don't tell TypeScript anything, because it doesn't see them as type checks. As a result, we end up with a lot of code like this:

if( isString(msg) ) {
  const words = (msg as string).split(' ');
}

where we have to typecast our variables, despite their type already having been checked immediately beforehand.

In order to fix this, TS lets you return a type predicate from a type-checking function rather than a simple boolean, and that lets it know what the human reader knows - if isString(msg) is true, then msg must be a string.

This switches all of our is functions to use type predicates, and removes all of the typecasts which are no longer necessary as a result of this change.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 22.45 KB (-0.12% 🔽)
@sentry/browser - Webpack 23.35 KB (-0.03% 🔽)
@sentry/react - Webpack 23.38 KB (-0.03% 🔽)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 29.89 KB (-0.1% 🔽)

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Nice, good refactor

@AbhiPrasad
Copy link
Member

we can even enforce this with an eslint rule, what do you think?

@lobsterkatie
Copy link
Member Author

we can even enforce this with an eslint rule, what do you think?

What exactly would you test for?

@AbhiPrasad
Copy link
Member

That isX functions in is.ts use type predicates instead of boolean returns.

maybe it’s not worth the hassle though.

@lobsterkatie
Copy link
Member Author

That isX functions in is.ts use type predicates instead of boolean returns.

maybe it’s not worth the hassle though.

Ah. Didn't know if you meant that or checking for unnecessary casts. In any case, yeah, I don't think checking the functions themselves is worth it - we add new ones so infrequently, and whoever does the next one will presumably see that all of the others use predicates and will hopefully follow suit.

@lobsterkatie lobsterkatie merged commit b99ee25 into master Nov 5, 2021
@lobsterkatie lobsterkatie deleted the kmclb-use-type-predicates-in-is-utils branch November 5, 2021 18:40
HazAT added a commit that referenced this pull request Nov 11, 2021
lobsterkatie added a commit that referenced this pull request Mar 23, 2022
This restores part of the work done in #4124, which then got reverted in #4149. The problem there was the predicates for `DOMError` and `DOMException`, both of which only exist in a browser context. This restores the predicates for `isError` and `isString`, which should be safe, because both `Error` and `string` exist in both browser and node contexts.
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.

None yet

2 participants