Skip to content

fix(lib): Fix RegExp constructor with string|RegExp and flags - #30586

Merged
Nathan Shively-Sanders (sandersn) merged 1 commit into
microsoft:masterfrom
ExE-Boss:lib/es2015/regexp-fix-constructor
Jun 13, 2019
Merged

Nathan Shively-Sanders (sandersn) merged 1 commit into
microsoft:masterfrom
ExE-Boss:lib/es2015/regexp-fix-constructor

Conversation

@ExE-Boss

@ExE-Boss ExE Boss (ExE-Boss) commented Mar 25, 2019 •

Copy link
Copy Markdown
Contributor

Previously:

export function replace(str: string, from: string | RegExp, to: string): string {
	return String(str).replace(RegExp(from, 'g'), to);
}

Would result in:

error TS2345: Argument of type 'string | RegExp' is not assignable to parameter of type 'string'.
  Type 'RegExp' is not assignable to type 'string'.

 return String(str).replace(RegExp(from, 'g'), to);
                                   ~~~~

This fixes that.

See also: #14107


review?(Nathan Shively-Sanders (@sandersn))

@ExE-Boss ExE Boss (ExE-Boss) mentioned this pull request Apr 1, 2019
4 of 13 tasks
@ExE-Boss

ExE Boss (ExE-Boss) commented Apr 1, 2019 •

Copy link
Copy Markdown
Contributor Author

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.

@sandersn

Copy link
Copy Markdown
Member

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.

@ExE-Boss

ExE Boss (ExE-Boss) commented Apr 2, 2019 •

Copy link
Copy Markdown
Contributor Author

Nathan Shively-Sanders (@sandersn)

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.

@sandersn

Copy link
Copy Markdown
Member

Daniel Rosenwasser (@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.

@ExE-Boss

ExE Boss (ExE-Boss) commented Apr 21, 2019 •

Copy link
Copy Markdown
Contributor Author

Nathan Shively-Sanders (@sandersn) There shouldn’t be any difference, except that my above snippet will work for projects using ES2015 or newer without needing to cast from to any.

Ar do a pointless typeof ternary expression, which is so much worse because it introduces a runtime performance penalty, rather than just a source code ugliness penalty: #14107 (comment).

@RyanCavanaugh

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test this

@typescript-bot

TypeScript Bot (typescript-bot) commented Apr 30, 2019 •

Copy link
Copy Markdown
Contributor

Heya Ryan Cavanaugh (@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.

@sandersn

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test this

@typescript-bot

TypeScript Bot (typescript-bot) commented Jun 13, 2019 •

Copy link
Copy Markdown
Contributor

Heya Nathan Shively-Sanders (@sandersn), 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.

@sandersn

Copy link
Copy Markdown
Member

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.

@sandersn
Nathan Shively-Sanders (sandersn) merged commit 81f7153 into microsoft:master Jun 13, 2019
@ExE-Boss
ExE Boss (ExE-Boss) deleted the lib/es2015/regexp-fix-constructor branch June 13, 2019 22:50
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants