{"id":3302,"date":"2022-03-09T10:45:20","date_gmt":"2022-03-09T18:45:20","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/typescript\/?p=3302"},"modified":"2022-03-09T10:45:20","modified_gmt":"2022-03-09T18:45:20","slug":"a-proposal-for-type-syntax-in-javascript","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/a-proposal-for-type-syntax-in-javascript\/","title":{"rendered":"A Proposal For Type Syntax in JavaScript"},"content":{"rendered":"<p><!-- A Proposal For Type Syntax in JavaScript --><\/p>\n<p>Today we&#8217;re excited to announce our support and collaboration on <a href=\"https:\/\/github.com\/giltayar\/proposal-types-as-comments\/\">a new Stage 0 proposal<\/a> to bring optional and erasable type syntax to JavaScript.\nBecause this new syntax wouldn&#8217;t change how surrounding code runs, it would effectively act <em>as comments<\/em>.\nWe think this has the potential to make TypeScript easier and faster to use for development at every scale.\nWe&#8217;d like to talk about why we&#8217;re pursuing this, and how this proposal works at a high level.<\/p>\n<h2>Background<\/h2>\n<p>One recent trend our team has seen in the JavaScript world is a demand for faster iteration time and a reduction of build steps.\nIn other words, &quot;make it faster and make it simpler&quot;.<\/p>\n<p>In some ways, this is already happening.\nThanks to the success of evergreen browsers, developers can often avoid compiling newer versions of JavaScript to run on older runtimes.\nTo some extent the same is also true of bundling &#8211; most browsers have built-in support for using modules, so bundling can be viewed as more of an optimization step than a necessity.\nThis has increasingly been the case, so how is TypeScript keeping up?<\/p>\n<p>If we go back to 2012 when TypeScript was first announced, the JavaScript world was drastically different!\nSome browsers shipped often, but not all.\nIt was unclear how long we&#8217;d be stuck with ancient versions of Internet Explorer, and that led to tools like bundlers and compilers gaining adoption.\nTypeScript was able to really thrive in the age where adding a build step to JavaScript was a given &#8211; after all, if you need to compile your JavaScript anyway, why not compile away your types too?\nBut if those trends we mentioned above continue, compiling away your types might be the only step between writing your TypeScript and running it, and we don&#8217;t want to be the ones standing in the way of a good developer experience!<\/p>\n<p>In some ways, our JavaScript support bridges the gap here, and maybe you&#8217;ve seen this if you use an editor like Visual Studio or Visual Studio Code.\nToday, you can create a <code>.js<\/code> file in your editor and start sprinkling in types in the form of JSDoc comments.<\/p>\n<pre class=\"lang:default decode:true\" style=\"background-color: #f0f0f0;padding: 10px;border-radius: 10px;\"><code><span style=\"color: #008000;\">\/**<\/span>\r\n<span style=\"color: #008000;\"> * @param a {number}<\/span>\r\n<span style=\"color: #008000;\"> * @param b {number}<\/span>\r\n<span style=\"color: #008000;\"> *\/<\/span>\r\n<span style=\"color: #0000FF;\">function<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #000;\">add<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #001080;\">a<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #001080;\">b<\/span><span style=\"color: #000000;\">) {<\/span>\r\n<span style=\"color: #000000;\">    <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #001080;\">a<\/span><span style=\"color: #000000;\"> + <\/span><span style=\"color: #001080;\">b<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n<\/code><\/pre>\n<p>Because these are just comments, they don&#8217;t change how your code runs at all &#8211; they&#8217;re just a form of documentation, but TypeScript uses them to give you a better JavaScript editing experience through things like code completions, refactorings, and more.\nYou can even <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/intro-to-js-ts.html\">add type-checking<\/a> by adding a <code>\/\/ @ts-check<\/code> comment to the top of your file, or running those files through the TypeScript compiler with <code>checkJs<\/code>.\nThis feature makes it incredibly convenient to get some of the TypeScript experience without a build step, and you can use it for small scripts, basic web pages, server code in Node.js, etc.<\/p>\n<p>Still, you&#8217;ll notice that this is a little verbose &#8211; we love how lightweight the inner-loop is for writing JavaScript, but we&#8217;re missing how convenient TypeScript makes it to just write types.<\/p>\n<p><em>So what if we had both?<\/em><\/p>\n<p>What if we could have something like TypeScript syntax which was totally ignored &#8211; sort of like comments &#8211; in JavaScript.<\/p>\n<pre class=\"lang:default decode:true\" style=\"background-color: #f0f0f0;padding: 10px;border-radius: 10px;\"><code><span style=\"color: #0000FF;\">function<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #000;\">add<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #001080;\">a<\/span><span style=\"color: #008000;\">: number<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #001080;\">b<\/span><span style=\"color: #008000;\">: number<\/span><span style=\"color: #000000;\">) {<\/span>\r\n<span style=\"color: #000000;\">    <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #001080;\">a<\/span><span style=\"color: #000000;\"> + <\/span><span style=\"color: #001080;\">b<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n<\/code><\/pre>\n<p>Our team believes there is <em>a lot<\/em> of potential here, and this month we&#8217;re hoping to <a href=\"https:\/\/github.com\/giltayar\/proposal-types-as-comments\">bring it forward in a proposal<\/a> to TC39, the ECMAScript standards committee!<\/p>\n<h2>How Would This Work?<\/h2>\n<p>When we&#8217;ve been asked &quot;when are types coming to JavaScript?&quot;, we&#8217;ve had to hesitate to answer.\nHistorically, the problem was that if you asked developers what they had in mind for types in JavaScript, you&#8217;d get many different answers.\nSome felt that types should be totally ignored, while others felt like they should have <em>some<\/em> meaning &#8211; possibly that they should enforce some sort of runtime validation, or that they should be introspectable, or that they should act as hints to the engine for optimization, and more!\nBut in the last few years we&#8217;ve seen people converge more towards a design that works well with the direction TypeScript has moved towards &#8211; that types are totally ignored and erasable syntax at runtime.\nThis convergence, alongside the broad use of TypeScript, made us feel more confident when several JavaScript and TypeScript developers outside of our core team approached us once more about a proposal called &quot;types as comments&quot;.<\/p>\n<p><a href=\"https:\/\/github.com\/giltayar\/proposal-types-as-comments\">The idea of this proposal<\/a> is that JavaScript could carve out a set of syntax for types that engines would <em>entirely ignore<\/em>, but which tools like TypeScript, Flow, and others could use.\nThis allows us to keep the things you love about TypeScript &#8211; its type-checking and editing experience &#8211; while removing the need for a build step in development.<\/p>\n<p>So when it comes to writing and <em>running<\/em> code, a developer&#8217;s inner-loop would look a little different.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2022\/03\/tac-announcement-today-proposed.svg\" alt=\"Today TypeScript goes through a process of compiling from .ts files to .js files, which then run in the browser. Our proposal is to simply have browsers run the input JavaScript code which would support type annotations.\"><\/p>\n<p>Meanwhile, writing code and <em>type-checking<\/em> would stay the same.\nA developer could get instant type-checking feedback in an editor with TypeScript support, run TypeScript on the command line, and add TypeScript as part of their CI tasks.\nThe biggest difference is that because we would not need a build step, we would <em>dramatically<\/em> lower the barrier to entry for JavaScript devs to experience the power of types and great tooling.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2022\/03\/tac-announcement-checking-behavior.svg\" alt=\"TypeScript and the VS family of editors can provide errors\/diagnostics for both TypeScript and JavaScript files that use type annotations. This process is roughly what the tools do today.\"><\/p>\n<p>To make this happen, JavaScript would minimally need to add syntax for things like type annotations on variables and functions, optionality modifiers (<code>?<\/code>) for parameters and class members, type declarations (<code>interface<\/code>s and <code>type<\/code> aliases), and type assertion operators (<code>as<\/code> and <code>!<\/code>) &#8211; all of which would have no effect on how the surrounding code is run.<\/p>\n<p>Things like visibility modifiers (e.g. <code>public<\/code>, <code>private<\/code>, and <code>protected<\/code>) might be in scope as well; however, enums, namespaces, and parameter properties would be out of scope for this proposal since they have observable runtime behavior.\nThose features could be proposed as separate ECMAScript features based on feedback, but our current goal is to support some large subset of TypeScript that we think could be a valuable addition to JavaScript.<\/p>\n<p>With this carve out, we&#8217;ve left room for type-checkers to innovate in ways that require new syntax.\nThat does mean that engines would happily run code with nonsensical types, but we believe type-checkers could (and should) be prescriptive and enforce stricter constraints than runtimes.\nCombined, this makes for a type syntax that could be customized across different checkers, or removable entirely if someone decides they&#8217;re not happy with TypeScript or any other type-checker.<\/p>\n<h2>What is this not?<\/h2>\n<p>It&#8217;s worth mentioning what this proposal <em>isn&#8217;t<\/em>.<\/p>\n<p>Our team isn&#8217;t proposing putting TypeScript&#8217;s type-checking in every browser and JavaScript runtime &#8211; nor are we proposing any new type-checker to be put in the browser.\nWe think doing that would cause problems for JavaScript and TypeScript users alike due to a range of issues, such as runtime performance, compatibility issues with existing TypeScript code, and the risk of halting innovation in the type-checking space.<\/p>\n<p>Instead, we&#8217;re just proposing syntax that is compatible with and motivated by TypeScript, which could be used by any type-checker, but which would skipped over by JavaScript engines.\nWe believe that this approach is the most promising for everyone, and would continue to allow TypeScript, Flow, and others to continue to innovate.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Given all this, we plan to present <a href=\"https:\/\/github.com\/giltayar\/proposal-types-as-comments\">this proposal<\/a> for <a href=\"https:\/\/tc39.es\/process-document\/\">Stage 1<\/a> at the upcoming March 2022 plenary meeting of TC39.\nWe&#8217;ll be doing so with the support and guidance from our co-champions of this proposal, <a href=\"https:\/\/github.com\/robpalme\">Rob Palmer<\/a> at Bloomberg and <a href=\"https:\/\/github.com\/romulocintra\">Romulo Cintra<\/a> at Igalia.<\/p>\n<p>Reaching Stage 1 would mean that the standards committee believes that supporting type syntax is worth considering for ECMAScript.\nThis isn&#8217;t a sure-fire thing &#8211; there are many valuable perspectives within the committee, and we do expect some amount of skepticism.\nA proposal like this will receive a lot of feedback and appropriate scrutiny.\nIt may involve lots design changes along the way, and may take years to yield results.<\/p>\n<p><em>But<\/em> if we pull this all off, we have the chance to make one of the most impactful improvements to the world of JavaScript.\nWe&#8217;re excited by that, and we hope you are too.<\/p>\n<p>If you&#8217;re interested in hearing more about the specifics and current direction, <a href=\"https:\/\/github.com\/giltayar\/proposal-types-as-comments\/\">head on over to the proposal repository<\/a>.\nWe look forward to hearing what you think!<\/p>\n<p>And lastly, the TypeScript team and the champions group would like to recognize and extend our thanks to all those who worked on <a href=\"https:\/\/github.com\/giltayar\/proposal-types-as-comments#prior-art\">prior art<\/a>, along with the contributors who reached out to help with types as comments, and especially <a href=\"https:\/\/github.com\/giltayar\/\">Gil Tayar<\/a> who helped spearhead it.\nWe&#8217;re grateful to be part of such a passionate community!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re excited to announce our support and collaboration on a new Stage 0 proposal to bring optional and erasable type syntax to JavaScript. Because this new syntax wouldn&#8217;t change how surrounding code runs, it would effectively act as comments. We think this has the potential to make TypeScript easier and faster to use for [&hellip;]<\/p>\n","protected":false},"author":381,"featured_media":1797,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Today we&#8217;re excited to announce our support and collaboration on a new Stage 0 proposal to bring optional and erasable type syntax to JavaScript. Because this new syntax wouldn&#8217;t change how surrounding code runs, it would effectively act as comments. We think this has the potential to make TypeScript easier and faster to use for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/3302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/users\/381"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/comments?post=3302"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/3302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/media\/1797"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/media?parent=3302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=3302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=3302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}