-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
fix(lib): Fix RegExp
constructor with string|RegExp
and flags
#30586
fix(lib): Fix RegExp
constructor with string|RegExp
and flags
#30586
Conversation
Obviously a less band‑aid solution would be identifying constructors that can be merged and dynamically merging them for union type calls, but I don’t really know how to do that. |
Would you mind explaining the problem more thoroughly. Normally we discuss a change like this with an issue, but this is minor enough it might be OK just to have a thorough explanation of the problem and the fix in the PR description. |
The issue here is that TypeScript doesn’t actually consider the two following cases equal: interface RegExpConstructor {
new (pattern: RegExp, flags?: string): RegExp;
(pattern: RegExp, flags?: string): RegExp;
new (pattern: string, flags?: string): RegExp;
(pattern: string, flags?: string): RegExp;
} interface RegExpConstructor {
new (pattern: RegExp | string, flags?: string): RegExp;
(pattern: RegExp | string, flags?: string): RegExp;
} The second one can be used with: var pattern: RegExp | string;
var regexp = RegExp(pattern, 'g'); while the first one can’t. #14107 describes this in more detail. |
@DanielRosenwasser do you have opinions? We need to look at the combined overload set and think about what happens if existing code starts resolving to a different overload. |
@sandersn There shouldn’t be any difference, except that my above snippet will work for projects using ES2015 or newer without needing to cast Ar do a pointless |
@typescript-bot test this |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 6f64fed. You can monitor the build here. It should now contribute to this PR's status checks. |
@typescript-bot test this |
RWC and user test suites are clean. I'm surprised that we don't have coverage of this anywhere in the test suite, but it's clean too. |
Previously:
Would result in:
This fixes that.
See also: #14107
review?(@sandersn)