Open
Description
openedon Jun 24, 2024
π Search Terms
- conditional
- regular expression
- regex
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
When a regular expression literal appears in a conditional, such as:
if (/abc?/) {
foo()
} else {
bar()
}
It would be helpful warn that bar()
is unreachable. The programmer likely meant to call /abc?/.test(...)
TypeScript could help catch this easy to make mistake
π Motivating Example
We recently were bitten by this in VS Code. It slipped through code review too
π» Use Cases
Help programmers catch an easy to make mistake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment