Title: JSDoc Type cast to `const` does not work in variable-like initializers · Issue #45463 · microsoft/TypeScript · GitHub
Open Graph Title: JSDoc Type cast to `const` does not work in variable-like initializers · Issue #45463 · microsoft/TypeScript
X Title: JSDoc Type cast to `const` does not work in variable-like initializers · Issue #45463 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms jsdoc cast as const 🕗 Version & Regression Information We have half-supported as const in JSDoc since it was added to TypeScript (~v3.5) ⏯ Playground Link Playground link with relevant code 💻 Code // @checkJs: t...
Open Graph Description: Bug Report 🔎 Search Terms jsdoc cast as const 🕗 Version & Regression Information We have half-supported as const in JSDoc since it was added to TypeScript (~v3.5) ⏯ Playground Link Playground link ...
X Description: Bug Report 🔎 Search Terms jsdoc cast as const 🕗 Version & Regression Information We have half-supported as const in JSDoc since it was added to TypeScript (~v3.5) ⏯ Playground Link Playground l...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/45463
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JSDoc Type cast to `const` does not work in variable-like initializers","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\njsdoc cast as const\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- We have half-supported `as const` in JSDoc since it was added to TypeScript (~v3.5)\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?target=99&jsx=0&filetype=js#code/PTAEAEGMAsFNINYCkDOAuUAXATgV1gLABQIEAhgDYUD2A7qhjvsaeCjgJaSaN6FEVYmUAA9QAXlDAAVNIiYAngAdYoAN6RqAO3YBfUNOAAKAIwBKANzERVosUHCFthRKmz5y1Ru16Dx88629kKgAF6uRjJy4Ioq6po6mPqGpmaWxKEWQA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\n// @checkJs: true\r\n// @allowJs: true\r\n// @strict: true\r\n\r\nlet x = /** @type {const} */(1); // error: cannot find name 'const'\r\nx; // any\r\n\r\nlet y;\r\ny = /** @type {const} */(1); // works\r\ny; // 1\r\n\r\nlet z = (/** @type {const} */(1)); works\r\nz; // 1\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nIn a JavaScript file, using a `/** @type {const} */( expr )` cast in the *initializer* of any variable-like declaration reports a \"Cannot find name 'const'\" error:\r\n- VariableDeclaration\r\n- PropertyDeclaration\r\n- PropertyAssignment\r\n- BindingElement\r\n- ParameterDeclaration\r\n\r\nIn any other position, the cast correctly behaves like a TypeScript `as const` assertion.\r\n\r\n### 🙂 Expected behavior\r\n\r\nIn a JavaScript file, using a `/** @type {const} */( expr )` cast should behave like TypeScript's `as const` in any position, not just random arguments.\r\n\r\n### Workaround:\r\n\r\nWhile `let x = /** @type {const} */({a: 1})` does not work, `let x = (/** @type {const} */({a: 1}))` does. This is because our logic to get the effective type annotation node for a variable-like declaration treats the type cast in the initializer as part of the declaration.\r\n","author":{"url":"https://github.com/rbuckton","@type":"Person","name":"rbuckton"},"datePublished":"2021-08-15T23:48:19Z","url":"https://github.com/microsoft/TypeScript/issues/45463"}
| 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:aa2d2c14-154f-4664-0bdc-da2be9add5bb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9998:25A204:D81A12:11DBA08:6B00F976 |
| html-safe-nonce | be420e49d44c4f5af221872d627dc0f64d03d632b11e545b902e0af19d3cfb3c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTk4OjI1QTIwNDpEODFBMTI6MTFEQkEwODo2QjAwRjk3NiIsInZpc2l0b3JfaWQiOiIyNTM3MDIwOTY0NzIzMDgwODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 05ba83b9f660dfbc6fa5e77f258cf6aa1efbcdb6bd057da88d2edbf7863fc32b |
| hovercard-subject-tag | issue:971251554 |
| 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/45463/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0e55325da54219240d0d0c14ac04b3c0613676f4c2f20d6f0c2a25898ef2ae2c/microsoft/TypeScript/issues/45463 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0e55325da54219240d0d0c14ac04b3c0613676f4c2f20d6f0c2a25898ef2ae2c/microsoft/TypeScript/issues/45463 |
| og:image:alt | Bug Report 🔎 Search Terms jsdoc cast as const 🕗 Version & Regression Information We have half-supported as const in JSDoc since it was added to TypeScript (~v3.5) ⏯ Playground Link Playground link ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rbuckton |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width