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
Seems like a duplicate of #4002 -- the function is intended to be used with strings and will convert any other input to a string. It won't throw an error but probably isn't doing what you intended. (/undefined/.test(undefined) is true!)
@andy-ms That is very interesting. I didn't consider the case of /undefined/.test(undefined) because my example is clearly not testing against /undefined/ so there is no unintended bug.
I'll leave the reader with this real world example that is deceiving at first glance:
TypeScript Version: 2.7.0-dev.20171206
Code
Expected behavior:
Should compile in
strict
mode.Actual behavior:
Does not compile in
strict
mode.Workaround
The workaround is to check if undefined and return a string before calling
regexp.test()
. For example:Solution
I believe the type definition for RegExp.test() should be the following
The reason being, that test() seem to return false in the case of null or undefined.
MDN does not mention that it throws.
The text was updated successfully, but these errors were encountered: