Title: False-positive errors regarding string array const · Issue #52684 · microsoft/TypeScript · GitHub
Open Graph Title: False-positive errors regarding string array const · Issue #52684 · microsoft/TypeScript
X Title: False-positive errors regarding string array const · Issue #52684 · microsoft/TypeScript
Description: Bug Report 🕗 Version & Regression Information It's not a regression, the same behaviour can be observed on TS 4 and 5. (for TS 4, using ts-toolbelt's Narrow function gives equivalent results) 💻 Code const staticPath1Level = ["home"] as c...
Open Graph Description: Bug Report 🕗 Version & Regression Information It's not a regression, the same behaviour can be observed on TS 4 and 5. (for TS 4, using ts-toolbelt's Narrow function gives equivalent results) 💻 Cod...
X Description: Bug Report 🕗 Version & Regression Information It's not a regression, the same behaviour can be observed on TS 4 and 5. (for TS 4, using ts-toolbelt's Narrow function gives equivalent re...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/52684
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"False-positive errors regarding string array const","articleBody":"# Bug Report\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nIt's not a regression, the same behaviour can be observed on TS 4 and 5. (for TS 4, using [ts-toolbelt](https://github.com/millsp/ts-toolbelt)'s Narrow function gives equivalent results)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\nconst staticPath1Level = [\"home\"] as const;\r\nconst staticPath2Level = [\"home\", \"user\"] as const;\r\nconst staticPath3Level = [\"home\", \"user\", \"downloads\"] as const;\r\n\r\nconst randomID = 'id' as string;\r\n\r\n// TS 5\r\ndeclare function foo\u003cconst T extends string[] | readonly string[]>(path: T): T;\r\n// TS 4\r\n// declare function foo\u003cT extends string[] | readonly string[]>(path: F.Narrow\u003cT>): T;\r\n\r\nconst a1 = foo([...staticPath1Level, randomID, 'doc.pdf']);\r\nconst a2 = foo([...staticPath2Level, randomID, 'doc.pdf']);\r\nconst a3 = foo([...staticPath3Level, randomID, 'doc.pdf']);\r\n\r\nconst b1 = foo([...staticPath1Level, randomID, 'folder', 'doc.pdf']);\r\nconst b2 = foo([...staticPath2Level, randomID, 'folder', 'doc.pdf']);\r\nconst b3 = foo([...staticPath3Level, randomID, 'folder', 'doc.pdf']);\r\n\r\nconst c1 = foo([...staticPath1Level, randomID, 'folder', 'subfolder', 'doc.pdf']);\r\nconst c2 = foo([...staticPath2Level, randomID, 'folder', 'subfolder', 'doc.pdf']);\r\nconst c3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'doc.pdf']);\r\n\r\nconst d1 = foo([...staticPath1Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);\r\nconst d2 = foo([...staticPath2Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);\r\nconst d3 = foo([...staticPath3Level, randomID, 'folder', 'subfolder', 'another-subfolder', 'doc.pdf']);\r\n\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nAll return types are correct, e.g.:\r\n```ts\r\nconst b3: readonly [\"home\", \"user\", \"downloads\", string, \"folder\", \"doc.pdf\"]\r\n```\r\n\r\nThe call for a2 has the following error (b3, c3 also have the same error type, adjusted for their parameters):\r\n> ts(2345)\r\n> Argument of type '[\"home\", \"user\", string, string]' is not assignable to parameter of type 'readonly [\"home\", \"user\", string, \"doc.pdf\"]'.\r\n> Type at position 3 in source is not compatible with type at position 3 in target.\r\n> Type 'string' is not assignable to type '\"doc.pdf\"'.\r\n\r\nThe call for b2 has the following error (c2, d2, d3 also have the same error type, adjusted for their parameters):\r\n> ts(2322)\r\n> Type 'string' is not assignable to type '\"folder\"'.\r\n\r\n\r\n### 🙂 Expected behavior\r\n\r\nDon't generate false-positive errors. Also, it's odd that there is no apparent pattern to when one of the errors is shown.\r\n\r\n### Workaround\r\n\r\nTo silence these errors, `as const` can be used, but again, it's not really obvious where or how many times.\r\nThis partial code sample generates no errors:\r\n```ts\r\nconst b2 = foo([...staticPath2Level, randomID, 'folder' as const, 'doc.pdf']);\r\nconst b3 = foo([...staticPath3Level, randomID, 'folder', 'doc.pdf' as const]);\r\n```\r\nThis partial code sample still has false-positive errors:\r\n```ts\r\nconst b2 = foo([...staticPath2Level, randomID, 'folder', 'doc.pdf' as const]);\r\nconst b3 = foo([...staticPath3Level, randomID, 'folder' as const, 'doc.pdf']);\r\n```","author":{"url":"https://github.com/mocanew","@type":"Person","name":"mocanew"},"datePublished":"2023-02-08T18:25:56Z","url":"https://github.com/microsoft/TypeScript/issues/52684"}
| 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:1f3bac77-a8df-4b58-9ab7-cfeaad47e986 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB22:24A0A:1D700CB:25276A9:6B018F9A |
| html-safe-nonce | dd318914440505cea194d3c0a91e3fadf2c3e74c1353a8603a46b2fc36dc45ad |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjIyOjI0QTBBOjFENzAwQ0I6MjUyNzZBOTo2QjAxOEY5QSIsInZpc2l0b3JfaWQiOiI1MTc1NTc3MjA3MTM2OTQ4MTIyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3cff93127241bdc7df43e0f72ded9ecb30d5298c0e62b59aaccc28e1773eaf5f |
| hovercard-subject-tag | issue:1576601369 |
| 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/52684/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4ea2183194c37d7f25b03d8a5bf7d84b5d7ac1d73a6337f7ba4f9d360a7d18f0/microsoft/TypeScript/issues/52684 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4ea2183194c37d7f25b03d8a5bf7d84b5d7ac1d73a6337f7ba4f9d360a7d18f0/microsoft/TypeScript/issues/52684 |
| og:image:alt | Bug Report 🕗 Version & Regression Information It's not a regression, the same behaviour can be observed on TS 4 and 5. (for TS 4, using ts-toolbelt's Narrow function gives equivalent results) 💻 Cod... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mocanew |
| 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