Title: Proposal: Easier Migration with Types in JavaScript Files · Issue #23906 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Easier Migration with Types in JavaScript Files · Issue #23906 · microsoft/TypeScript
X Title: Proposal: Easier Migration with Types in JavaScript Files · Issue #23906 · microsoft/TypeScript
Description: This issue is the dual of #22665. Background In last week's design meeting, we discussed #22665 and it seemed like we had some positive sentiment around the proposal. Specifically, issues where users hit an expressivity wall or mismatch ...
Open Graph Description: This issue is the dual of #22665. Background In last week's design meeting, we discussed #22665 and it seemed like we had some positive sentiment around the proposal. Specifically, issues where use...
X Description: This issue is the dual of #22665. Background In last week's design meeting, we discussed #22665 and it seemed like we had some positive sentiment around the proposal. Specifically, issues where...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/23906
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Easier Migration with Types in JavaScript Files","articleBody":"This issue is the dual of #22665.\r\n\r\n# Background\r\n\r\nIn last week's design meeting, we discussed #22665 and it seemed like we had some positive sentiment around the proposal. Specifically, issues where users hit an expressivity wall or mismatch in `.js` when migrating to `.ts` files provide a compelling case.\r\n\r\nHowever, there are certain issues with the proposal:\r\n\r\n* It still requires users to make a file extension change from `.js` to `.ts`, which usually implies some change in build steps.\r\n* It provides no easy migration path from other type systems with annotation syntax used in `.js` files (see https://github.com/Microsoft/TypeScript-Babel-Starter/issues/1).\r\n* It introduces the concept of a \"diluted/loose\" TypeScript experience which can make things more confusing.\r\n\r\n# Proposal\r\n\r\nWe can introduce a new mode that enables TypeScript-specific constructs in `.js` files. This new mode would allow things like type annotations, `interface`s, class property annotations, etc. to be declared in `.js` files. Users would have to explicitly opt-in to this mode for transitionary purposes, with the end goal of migrating to TypeScript.\r\n\r\n## Supported constructs\r\n\r\nThe following constructs from TypeScript would be supported:\r\n\r\n* Type annotations (`: Foo`)\r\n* Interfaces (`interface Foo { /*...*/ }`)\r\n* Type alias (`type Foo = \"hello\" | \"world\"`)\r\n* `as`-style type assertions (`xyz as SomeType`)\r\n* Non-nullable type assertions (`x!.bar`)\r\n* Definite assignment assertions (`let x!: number`)\r\n* Enums (`enum E { /*...*/ }`)\r\n* Import aliases and export aliases:\r\n * `import\r\n\r\n## Unsupported constructs\r\n\r\nThe following would not be supported to encourage users to write ECMAScript modules and standard enums that utilize late-binding.\r\n\r\n* Namespaces\r\n* `const` enums\r\n\r\n## Existing typed `.js` file semantics\r\n\r\nA JavaScript file still has the same specialized semantics including understanding things like CommonJS modules, JSDoc type annotations, ES5-style constructor functions, object literals potentially being open-ended, etc. However, in the presence of an analogous TypeScript construct, the TypeScript construct always wins. For example, the following declares `x` as type `number`, and potentially issues an error on the JSDoc.\r\n\r\n```ts\r\n/**\r\n * @type {string}\r\n */\r\nvar x: number;\r\n```\r\n\r\n# Drawbacks\r\n\r\nThis proposal avoids the aforementioned issues with #22665, but has some new drawbacks.\r\n\r\nFor one, it seems like there's little-to-no advantage to moving from a `.js` file to a `.ts` file, even though TypeScript is easier to reason about and make assumptions on (whereas much of our JavaScript semantics are more \"best-effort\").\r\n\r\nThe other issue, which is likely much bigger, is that this conflates where JavaScript ends and TypeScript begins. While the intent here is to make migration easier, enabling this functionality could be perceived poorly as an attempt to extend the language inappropriately. While we hope users understand this is **not** the case, we are extremely open to feedback from the community on this issue.\r\n\r\nIf this flag was called something like `--jsMigrationMode`, perhaps we could be explicit on the intent and also signal to users that having this flag on is not ideal.\r\n\r\n## Existing Precedent\r\n\r\nCommon usage of JSX and Flow in `.js` files in the Babel ecosystem (as opposed to using the `.jsx` file extensions) provides a reasonable precedent for supporting TypeScript constructs in `.js` files.","author":{"url":"https://github.com/DanielRosenwasser","@type":"Person","name":"DanielRosenwasser"},"datePublished":"2018-05-04T22:05:06Z","url":"https://github.com/microsoft/TypeScript/issues/23906"}
| 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:d09a8f12-272d-fd48-446d-cd28cb4af215 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C758:34CAB:3224233:4028046:6B01DF7F |
| html-safe-nonce | ac7a472f172cc5a6f736a547e521954407d32c38d9885207c3c83b990cb6bcf1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzU4OjM0Q0FCOjMyMjQyMzM6NDAyODA0Njo2QjAxREY3RiIsInZpc2l0b3JfaWQiOiIxNzI4NzY2NTI3ODk5MDMzNDcxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d2d97d5115d3f091ce1c2e2d455b5df0d545490dda195da3a68029ae8cd3647f |
| hovercard-subject-tag | issue:320443934 |
| 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/23906/issue_layout |
| twitter:image | https://opengraph.githubassets.com/38a5ff4dc6af8d746da57258025649cfb539fdba07ced739051752ab5c08a1d4/microsoft/TypeScript/issues/23906 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/38a5ff4dc6af8d746da57258025649cfb539fdba07ced739051752ab5c08a1d4/microsoft/TypeScript/issues/23906 |
| og:image:alt | This issue is the dual of #22665. Background In last week's design meeting, we discussed #22665 and it seemed like we had some positive sentiment around the proposal. Specifically, issues where use... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DanielRosenwasser |
| 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