Title: make Strings.startsWith to support ${Prefix}${string} · Issue #54971 · microsoft/TypeScript · GitHub
Open Graph Title: make Strings.startsWith to support ${Prefix}${string} · Issue #54971 · microsoft/TypeScript
X Title: make Strings.startsWith to support ${Prefix}${string} · Issue #54971 · microsoft/TypeScript
Description: lib Update Request Sometimes, we might make a more specific definition on string type to avoid confusion in programming. For example: const statePrefix = `State/` as const type State = `${typeof statePrefix}${string}` const customerIdPre...
Open Graph Description: lib Update Request Sometimes, we might make a more specific definition on string type to avoid confusion in programming. For example: const statePrefix = `State/` as const type State = `${typeof st...
X Description: lib Update Request Sometimes, we might make a more specific definition on string type to avoid confusion in programming. For example: const statePrefix = `State/` as const type State = `${typeof st...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/54971
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"make Strings.startsWith to support ${Prefix}${string}","articleBody":"# lib Update Request\r\n\r\nSometimes, we might make a more specific definition on string type to avoid confusion in programming. For example:\r\n```\r\nconst statePrefix = `State/` as const\r\ntype State = `${typeof statePrefix}${string}`\r\nconst customerIdPrefix = `Customer/` as const\r\ntype CustomerId = `${typeof customerIdPrefix}`\r\n```\r\nIn this way, we can distinguish state and customerId, rather than using `string` everywhere. For example, we can clearly know a structure of a dictionary like\r\n```\r\n// Avoid confusion of Map\u003cstring, Map\u003cstring, boolean>>\r\nconst IsActiveCustomerMap = new Map\u003cState\u003cMap\u003cCustomerId, boolean>>>()\r\n```\r\n\r\nSo, I suggest adding `startsWith` definition to understand literal types. I would suggest this definition added to the library in addition to the original definition: \r\n```\r\n startsWith\u003cT extends string>(searchString: T, position?: 0): this is `${T}${string};\r\n```\r\nThis definition can help us simplify the specific string definition.\r\n\r\n## Configuration Check\r\n\r\nMy compilation *target* is `ES2015` and my *lib* is `the default`.\r\n\r\n## Missing / Incorrect Definition\r\n\r\nAdd to Strings:\r\n```\r\n startsWith\u003cT extends string>(searchString: T, position?: 0): this is `${T}${string};\r\n```\r\n\r\n## Sample Code\r\n\r\n```\r\nfunction mkPrefix\u003cT extends string>(prefix: T, key: string): `${T}${string}` {\r\n if (key.startsWith(prefix)) return key;\r\n return `${prefix}${key}`\r\n}\r\n```\r\n\r\n\r\n## Documentation Link\r\n","author":{"url":"https://github.com/zhujinxuan","@type":"Person","name":"zhujinxuan"},"datePublished":"2023-07-11T15:32:21Z","url":"https://github.com/microsoft/TypeScript/issues/54971"}
| 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:f809bae6-3f86-c9d1-6b3e-558505fa4839 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D72C:31F706:BFC73A:104A7E2:6B048976 |
| html-safe-nonce | f779dc2392db8b42f301e99c8ba1ef59f3e97127071ac20afbdb65028581d1fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzJDOjMxRjcwNjpCRkM3M0E6MTA0QTdFMjo2QjA0ODk3NiIsInZpc2l0b3JfaWQiOiIxMTM0NTU3ODg4Mzk2OTUzOTc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2ebe38803ab4249c357405f653cd3c8670351d58a1cd98b75d5471c8081a3147 |
| hovercard-subject-tag | issue:1799207556 |
| 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/54971/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bbb9ed3a4b7930052ece5b9089b449688b9dc890e2d369cede1a843b0da3f18c/microsoft/TypeScript/issues/54971 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bbb9ed3a4b7930052ece5b9089b449688b9dc890e2d369cede1a843b0da3f18c/microsoft/TypeScript/issues/54971 |
| og:image:alt | lib Update Request Sometimes, we might make a more specific definition on string type to avoid confusion in programming. For example: const statePrefix = `State/` as const type State = `${typeof st... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zhujinxuan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1d5c152376e38b7d2e37f3d5179be29a73bee93b85110da28a4eebe1159cdc70 |
| 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 | f5017e8ff4ea25a8011fcf0f2a0e430c60dc86c8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width