Skip to content

Accept union replacement values in String.replace - #64330

Draft
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
mainfrom
copilot/update-string-replace-typing
Draft

Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
mainfrom
copilot/update-string-replace-typing

Conversation

Copilot AI commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Description

String.prototype.replace accepts either a string or callback replacement, but its overloads rejected variables typed as their union.

  • Replace separate overloads with a union-typed replacement argument.
  • Apply the same contract to custom [Symbol.replace] search objects.
  • Add a compiler regression test for both paths.
declare const replacement: string | ((match: string) => string);

"text".replace(/./, replacement);

@typescript-automation typescript-automation Bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Sep 19, 2026
Copilot AI linked an issue Sep 19, 2026 that may be closed by this pull request
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 19, 2026
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix convoluted typing for String.replace Accept union replacement values in String.replace Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Notes about String.replace

2 participants