You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ref(utils): Use type predicates in is utility functions (#4124)
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[1] 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.
[1] https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
0 commit comments