Title: Notes about `String.replace` · Issue #54387 · microsoft/TypeScript · GitHub
Open Graph Title: Notes about `String.replace` · Issue #54387 · microsoft/TypeScript
X Title: Notes about `String.replace` · Issue #54387 · microsoft/TypeScript
Description: (Moved from rom microsoft/TypeScript-DOM-lib-generator#1562) I am using TypeScript 5.0.4. I have noticed the typing of String.replace seem oddly convoluted. From lib.es5.d.ts: interface String { replace(searchValue: string | RegExp, repl...
Open Graph Description: (Moved from rom microsoft/TypeScript-DOM-lib-generator#1562) I am using TypeScript 5.0.4. I have noticed the typing of String.replace seem oddly convoluted. From lib.es5.d.ts: interface String { re...
X Description: (Moved from rom microsoft/TypeScript-DOM-lib-generator#1562) I am using TypeScript 5.0.4. I have noticed the typing of String.replace seem oddly convoluted. From lib.es5.d.ts: interface String { re...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/54387
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Notes about `String.replace`","articleBody":"(Moved from rom https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1562)\r\n\r\nI am using TypeScript 5.0.4.\r\nI have noticed the typing of `String.replace` seem oddly convoluted.\r\n\r\nFrom `lib.es5.d.ts`:\r\n```ts\r\ninterface String {\r\n replace(searchValue: string | RegExp, replaceValue: string): string;\r\n replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;\r\n}\r\n```\r\n\r\nFrom `lib.es2015.symbol.wellknown.d.ts`:\r\n```ts\r\ninterface String {\r\n replace(searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string): string;\r\n replace(searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string): string;\r\n}\r\n```\r\n\r\nLike the first parameter is declared as an union, I would expect the second to be the same, at least in the `lib.es5` version.\r\n\r\nNotably, this makes the following code not compile:\r\n\r\n```ts\r\n// The second argument is an union of replaceValue and replacer from lib.es5\r\nconst replacementSteps: ([RegExp, string | ((substring: string, ...args: any[]) => string)])[] = [\r\n [/(a|b)/gi, \"a\"],\r\n [/(c|d)/gi, match => match.toUpperCase()],\r\n];\r\n\r\nlet myString = \"abcdef\";\r\n\r\nfor (const [searchValue, replacement] of replacementSteps)\r\n myString = myString.replace(searchValue, replacement); // TS2345 - the second parameter is considered invalid\r\n```\r\n\r\nBy adding this into my declaration, the code works:\r\n\r\n```ts\r\ndeclare interface String {\r\n replace(searchValue: string | RegExp, replaceValue: string | ((substring: string, ...args: any[]) => string)): string;\r\n}\r\n```\r\n\r\nI cannot propose a solution other than my last hack, and of course the variants with `Symbol` would have to be taken into account. Please consider this just a note.\r\n\r\nSee also this: https://github.com/microsoft/TypeScript/issues/54223","author":{"url":"https://github.com/m-gallesio","@type":"Person","name":"m-gallesio"},"datePublished":"2023-05-25T07:31:59Z","url":"https://github.com/microsoft/TypeScript/issues/54387"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a5cb209d-699f-1716-09ca-8454b8b83da3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C502:157C65:2F1A30B:3ED7F29:6B01E242 |
| html-safe-nonce | c2d938e4cdcf40627cf8cf61085c2572874862ae1d36074ed4f4a8e608a944bd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTAyOjE1N0M2NToyRjFBMzBCOjNFRDdGMjk6NkIwMUUyNDIiLCJ2aXNpdG9yX2lkIjoiMzI3NTY0OTA4MjE0NDc3Mzc4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4ebc7afb4e7f22a820baa64b3b0e3876ba2d1a0b4694042cc95dc5cb69384d04 |
| hovercard-subject-tag | issue:1725254013 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/54387/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b571659e153b57e3d56f61e0429c07772bc1864a622510ba9c06e12580515de1/microsoft/TypeScript/issues/54387 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b571659e153b57e3d56f61e0429c07772bc1864a622510ba9c06e12580515de1/microsoft/TypeScript/issues/54387 |
| og:image:alt | (Moved from rom microsoft/TypeScript-DOM-lib-generator#1562) I am using TypeScript 5.0.4. I have noticed the typing of String.replace seem oddly convoluted. From lib.es5.d.ts: interface String { re... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | m-gallesio |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width