Title: Proposal: Easier Migration with Loose Mode in TypeScript Files · Issue #22665 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Easier Migration with Loose Mode in TypeScript Files · Issue #22665 · microsoft/TypeScript
X Title: Proposal: Easier Migration with Loose Mode in TypeScript Files · Issue #22665 · microsoft/TypeScript
Description: This issue is the dual of #23906. Background TypeScript has always aimed at making migration from JavaScript as easy as possible; however, even today there exists a bit of an awkward gap where you must rewrite your JavaScript code to put...
Open Graph Description: This issue is the dual of #23906. Background TypeScript has always aimed at making migration from JavaScript as easy as possible; however, even today there exists a bit of an awkward gap where you ...
X Description: This issue is the dual of #23906. Background TypeScript has always aimed at making migration from JavaScript as easy as possible; however, even today there exists a bit of an awkward gap where you ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22665
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Easier Migration with Loose Mode in TypeScript Files","articleBody":"This issue is the dual of #23906.\r\n\r\n# Background\r\n\r\nTypeScript has always aimed at making migration from JavaScript as easy as possible; however, even today there exists a bit of an awkward gap where you must rewrite your JavaScript code to put it in a state where it's \"ready\" to be converted to TypeScript. As an example, users might start annotating their code with JSDoc to get better analysis in `.js` files, but once they switch their file extensions to `.ts`, TypeScript pretends like it doesn't understand those annotations anymore.\r\n\r\n# Proposal\r\n\r\nTypeScript can provide a new \"loose\" mode (which we'll refer to as `looseTs`). This loose mode can be seen as a hybrid between the `allowJs`/Salsa language service support for JavaScript, along with TypeScript itself.\r\n\r\nLet's explore what would this experience would look like.\r\n\r\n## Special JavaScript Declarations Are Allowed\r\n\r\nTypeScript would understand certain constructs that are currently special cased in JS files. For example, the following construct would create what TypeScript effectively sees as a constructor.\r\n\r\n```ts\r\nfunction Foo() {\r\n this.x = 10;\r\n this.y = 20;\r\n this.z = 30;\r\n}\r\n\r\nnew Foo().x + new Foo().y + new Foo().z // all well-typed as 'number'\r\n```\r\n\r\nOur language service could provide tooling to help refactor this to a more canonical sort of class.\r\n\r\n## Type Annotations & Declarations\r\n\r\nAll TypeScript constructs would continue to work, but so would JSDoc annotations & declarations.\r\n\r\n```ts\r\n/**\r\n * @typedef {PersonName} string\r\n */\r\n\r\nexport interface Person {\r\n // valid to reference JSDoc declarations in the same file\r\n name: PersonName;\r\n}\r\n```\r\n\r\nTypeScript constructs would always take precedent over their JSDoc counterparts when they potentially conflict. For example, the following provides an error at worst, but will always consider `x` to be a `string`:\r\n\r\n```ts\r\n/**\r\n * @type {number}\r\n */\r\nvar x: string;\r\n```\r\n\r\n## Class Properties\r\n\r\nToday, TypeScript requires all classes to have property declarations.\r\n\r\nFuture versions of ECMAScript might *allow* property declarations.\r\n\r\nThe story here is: pick one. If classes contain no property declarations, then we'll fall back to Salsa-style inference from the constructor body. If any property declarations are present, then any access to `this` with a member that hasn't been declared is an error.\r\n\r\n```ts\r\n// Good!\r\nclass AllDeclared {\r\n a: number;\r\n constructor() {\r\n this.a = 0;\r\n }\r\n}\r\n\r\n// Good!\r\n// Also, users get a quick fix.\r\nclass OnlyInitialized {\r\n constructor() {\r\n this.a = 0;\r\n }\r\n}\r\n\r\n// Bad!\r\nclass MixedDeclarationsAndInitialization {\r\n a: number;\r\n constructor() {\r\n this.a = 10;\r\n this.b = \"hallo\";\r\n }\r\n}\r\n```\r\n\r\nWe can provide tooling to help migrate users to TypeScript code that declares all of the initialized properties.\r\n\r\n## Best-Effort Completions\r\n\r\nLike in Salsa, we should provide loose completions for things like `any` by default. This might be editor-configurable, but see #5334 for some more discussion.\r\n\r\n## More?\r\n\r\nYour ideas here!\r\n\r\n# Drawbacks\r\n\r\nOne argument is that this could potentially dilute the value of the language, similar to concerns around `// @ts-ignore`.\r\n\r\nAnother related argument has to do with cognitive overhead. Our heuristics around understanding JavaScript constructs in Salsa (our JS language service) feels somewhat opaque. While we can document what special constructs we support, TypeScript's current model is significantly simpler. It might not be clear what features \"proper TypeScript\" actually supports (i.e. what exactly are you giving up when you turn off `looseTs`?)\r\n\r\n# Alternatives\r\n\r\nWe may want to consider alternatives before we commit to something like this.\r\n\r\n## Type Annotations in JavaScript Files\r\n\r\nThis proposal has proposed the idea of understanding Salsa/JS constructs in `.ts` files, but one could approach it from the other direction: Salsa could support type annotations in `.js` files instead under a specific mode. Our main concern here has been the conflation between what is possible in JavaScript today, and what constructs are specific to TypeScript. Additionally, providing non-standard constructs in `.js` files might give users the wrong impression over what we're trying to achieve (to clarify, in this issue, we're here to provide tooling for JavaScript users and make it easier to migrate to TypeScript).\r\n\r\n## Tooling Tooling Tooling Tooling\r\n\r\nNow that we have suggestion diagnostics, we can potentially provide suggestions & tooling to just fix \"legacy\" JS constructs in the language service once you've moved to TypeScript. The counter-point here is that users will still likely experience the \"sea of red\" problem when you first switch a file over to `.ts`.","author":{"url":"https://github.com/DanielRosenwasser","@type":"Person","name":"DanielRosenwasser"},"datePublished":"2018-03-16T23:18:50Z","url":"https://github.com/microsoft/TypeScript/issues/22665"}
| 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:27f24130-6646-495d-c61b-60a5d7748a87 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C78E:20AA01:38200E9:4749306:6B01DCC4 |
| html-safe-nonce | 007b895a9b8f398db2fab14f8277e3deb48710c8e8844ec5da2c876db6bce93d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzhFOjIwQUEwMTozODIwMEU5OjQ3NDkzMDY6NkIwMURDQzQiLCJ2aXNpdG9yX2lkIjoiOTExNTk5MTM0OTM3NDYwNjUzMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a627f831f0cc6d6e4e3d12b9e596c9d6350ec01adc82e14651970c40372244f5 |
| hovercard-subject-tag | issue:306103421 |
| 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/22665/issue_layout |
| twitter:image | https://opengraph.githubassets.com/979574aff7e3cfd1d7c4784aa42902a5ede0111e84e1b30eaac05094f3c3f254/microsoft/TypeScript/issues/22665 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/979574aff7e3cfd1d7c4784aa42902a5ede0111e84e1b30eaac05094f3c3f254/microsoft/TypeScript/issues/22665 |
| og:image:alt | This issue is the dual of #23906. Background TypeScript has always aimed at making migration from JavaScript as easy as possible; however, even today there exists a bit of an awkward gap where you ... |
| 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