https://devblogs.microsoft.com/typescript/a-proposal-for-type-syntax-in-javascript/ Skip to main content [RE1Mu3b] Microsoft TypeScript TypeScript TypeScript * Home * DevBlogs * Developer + Visual Studio + Visual Studio Code + Visual Studio for Mac + DevOps + Developer support + CSE Developer + Engineering@Microsoft + Azure SDK + IoT + Command Line + Perf and Diagnostics + Dr. International + Notification Hubs + Math in Office * Technology + DirectX + PIX + SurfaceDuo + Startups + Sustainable Engineering + Windows AI Platform * Languages + C++ + C# + F# + Visual Basic + TypeScript + PowerShell Community + PowerShell Team + Python + Q# + JavaScript + Java + Java Blog in Chinese * .NET + .NET + .NET MAUI + Blazor + ASP.NET + NuGet + Xamarin * Platform Development + #ifdef Windows + Apps for Windows + Azure Depth Platform + Azure Government + Bing Dev Center + Microsoft Edge Dev + Microsoft Azure + Microsoft 365 Developer + Old New Thing + Windows MIDI and Music dev + Windows Search Platform * Data Development + Azure Cosmos DB + Azure Data Studio + Azure SQL + OData + Revolutions R + SQL Server Data Tools * More [ ] Search Search Cancel A Proposal For Type Syntax in JavaScript [png] Daniel March 9th, 20224 Today we'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'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 development at every scale. We'd like to talk about why we're pursuing this, and how this proposal works at a high level. Background One recent trend our team has seen in the JavaScript world is a demand for faster iteration time and a reduction of build steps. In other words, "make it faster and make it simpler". In some ways, this is already happening. Thanks to the success of evergreen browsers, developers can often avoid compiling newer versions of JavaScript to run on older runtimes. To some extent the same is also true of bundling - most browsers have built-in support for using modules, so bundling can be viewed as more of an optimization step than a necessity. This has increasingly been the case, so how is TypeScript keeping up? If we go back to 2012 when TypeScript was first announced, the JavaScript world was drastically different! Some browsers shipped often, but not all. It was unclear how long we'd be stuck with ancient versions of Internet Explorer, and that led to tools like bundlers and compilers gaining adoption. TypeScript was able to really thrive in the age where adding a build step to JavaScript was a given - after all, if you need to compile your JavaScript anyway, why not compile away your types too? But 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't want to be the ones standing in the way of a good developer experience! In some ways, our JavaScript support bridges the gap here, and maybe you've seen this if you use an editor like Visual Studio or Visual Studio Code. Today, you can create a .js file in your editor and start sprinkling in types in the form of JSDoc comments. /** * @param a {number} * @param b {number} */ function add(a, b) { return a + b; } Because these are just comments, they don't change how your code runs at all - they'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. You can even add type-checking by adding a // @ts-check comment to the top of your file, or running those files through the TypeScript compiler with checkJs. This 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. Still, you'll notice that this is a little verbose - we love how lightweight the inner-loop is for writing JavaScript, but we're missing how convenient TypeScript makes it to just write types. So what if we had both? What if we could have something like TypeScript syntax which was totally ignored - sort of like comments - in JavaScript. function add(a: number, b: number) { return a + b; } Our team believes there is a lot of potential here, and this month we're hoping to bring it forward in a proposal to TC39, the ECMAScript standards committee! How Would This Work? When we've been asked "when are types coming to JavaScript?", we've had to hesitate to answer. Historically, the problem was that if you asked developers what they had in mind for types in JavaScript, you'd get many different answers. Some felt that types should be totally ignored, while others felt like they should have some meaning - 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! But in the last few years we've seen people converge more towards a design that works well with the direction TypeScript has moved towards - that types are totally ignored and erasable syntax at runtime. This 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 "types as comments". The idea of this proposal is that JavaScript could carve out a set of syntax for types that engines would entirely ignore, but which tools like TypeScript, Flow, and others could use. This allows us to keep the things you love about TypeScript - its type-checking and editing experience - while removing the need for a build step in development. So when it comes to writing and running code, a developer's inner-loop would look a little different. 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. Meanwhile, writing code and type-checking would stay the same. A 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. The biggest difference is that because we would not need a build step, we would dramatically lower the barrier to entry for JavaScript devs to experience the power of types and great tooling. 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. To make this happen, JavaScript would minimally need to add syntax for things like type annotations on variables and functions, optionality modifiers (?) for parameters and class members, type declarations (interfaces and type aliases), and type assertion operators (as and !) - all of which would have no effect on how the surrounding code is run. Things like visibility modifiers (e.g. public, private, and protected) 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. Those 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. With this carve out, we've left room for type-checkers to innovate in ways that require new syntax. That 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. Combined, this makes for a type syntax that could be customized across different checkers, or removable entirely if someone decides they're not happy with TypeScript or any other type-checker. What is this not? It's worth mentioning what this proposal isn't. Our team isn't proposing putting TypeScript's type-checking in every browser and JavaScript runtime - nor are we proposing any new type-checker to be put in the browser. We 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. Instead, we'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. We believe that this approach is the most promising for everyone, and would continue to allow TypeScript, Flow, and others to continue to innovate. What's next? Given all this, we plan to present this proposal for Stage 1 at the upcoming March 2022 plenary meeting of TC39. We'll be doing so with the support and guidance from our co-champions of this proposal, Rob Palmer at Bloomberg and Romulo Cintra at Igalia. Reaching Stage 1 would mean that the standards committee believes that supporting type syntax is worth considering for ECMAScript. This isn't a sure-fire thing - there are many valuable perspectives within the committee, and we do expect some amount of skepticism. A proposal like this will receive a lot of feedback and appropriate scrutiny. It may involve lots design changes along the way, and may take years to yield results. But if we pull this all off, we have the chance to make one of the most impactful improvements to the world of JavaScript. We're excited by that, and we hope you are too. If you're interested in hearing more about the specifics and current direction, head on over to the proposal repository. We look forward to hearing what you think! And lastly, the TypeScript team and the champions group would like to recognize and extend our thanks to all those who worked on prior art, along with the contributors who reached out to help with types as comments, and especially Gil Tayar who helped spearhead it. We're grateful to be part of such a passionate community! [png] Daniel Rosenwasser Senior Program Manager, TypeScript Follow Read next Announcing TypeScript 4.6 Today we're announcing the availability of TypeScript 4.6. If you're not yet familiar with TypeScript, it's a language that builds on JavaScript and adds syntax for ... [png] Daniel Rosenwasser February 28, 2022 0 comment Announcing TypeScript 4.6 RC Today we're excited to announce our Release Candidate (RC) of TypeScript 4.6! Between now and the stable release of TypeScript 4.6, we expect no further changes apart ... [png] Daniel Rosenwasser February 11, 2022 0 comment 4 comments Leave a commentCancel reply Log in to join the discussion. * [png] Steve Davis March 9, 2022 12:21 pm collapse this comment I love this. Log in to Reply * [png] Ashley Claymore March 9, 2022 12:51 pm collapse this comment Most excellent! Log in to Reply * [png] Daniel Brain March 9, 2022 12:59 pm collapse this comment Unless this is a 1:1 match with TypeScript, we'll still need to transpile everything right? Otherwise people who don't want to transpile will need to know and use a subset of TS syntax. That problem will be exacerbated by any new TS version which introduces new syntax, keywords, and so on. Log in to Reply * [png] Gabriel Dibble March 9, 2022 1:53 pm collapse this comment Log in to Reply Top Bloggers [png] Daniel Rosenwasser Senior Program Manager [png] Orta Therox Engineer on the TypeScript Compiler Archive March 2022 February 2022 January 2022 November 2021 October 2021 August 2021 July 2021 May 2021 April 2021 March 2021 February 2021 January 2021 November 2020 September 2020 August 2020 June 2020 May 2020 April 2020 March 2020 February 2020 January 2020 November 2019 October 2019 August 2019 July 2019 June 2019 May 2019 March 2019 January 2019 December 2018 November 2018 October 2018 August 2018 July 2018 May 2018 March 2018 January 2018 October 2017 August 2017 June 2017 May 2017 April 2017 March 2017 February 2017 December 2016 November 2016 September 2016 August 2016 July 2016 June 2016 May 2016 March 2016 February 2016 January 2016 November 2015 September 2015 July 2015 June 2015 April 2015 March 2015 February 2015 January 2015 November 2014 October 2014 July 2014 May 2014 April 2014 February 2014 December 2013 October 2013 September 2013 August 2013 June 2013 April 2013 March 2013 February 2013 January 2013 December 2012 November 2012 Relevant Links The TypeScript Website TypeScript on GitHub TypeScript on Twitter Stay informed Login Insert/edit link Close Enter the destination URL URL [ ] Link Text [ ] [ ] Open link in a new tab Or link to existing content Search [ ] No search term specified. Showing recent items. Search or use up and down arrow keys to select an item. Cancel [Add Link] Code Block x Paste your code snippet [ ] Cancel Ok What's new * Surface Pro 8 * Surface Laptop Studio * Surface Pro X * Surface Go 3 * Surface Duo 2 * Surface Pro 7+ * Windows 11 apps * HoloLens 2 Microsoft Store * Account profile * Download Center * Microsoft Store support * Returns * Order tracking * Virtual workshops and training * Microsoft Store Promise * Flexible Payments Education * Microsoft in education * Office for students * Office 365 for schools * Deals for students & parents * Microsoft Azure in education * Education consultation appointment Business * Azure * AppSource * Automotive * Government * Healthcare * Manufacturing * Financial services * Retail Developer & IT * Microsoft Visual Studio * Windows Dev Center * Developer Center * Microsoft developer program * Channel 9 * Microsoft 365 Dev Center * Microsoft 365 Developer Program * Microsoft Garage Company * Careers * About Microsoft * Company news * Privacy at Microsoft * Investors * Diversity and inclusion * Accessibility * Security English (United States) * Sitemap * Contact Microsoft * Privacy * Manage cookies * Terms of use * Trademarks * Safety & eco * About our ads * (c) Microsoft 2022