{"id":2880,"date":"2021-02-23T13:56:54","date_gmt":"2021-02-23T21:56:54","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/typescript\/?p=2880"},"modified":"2026-02-08T17:15:45","modified_gmt":"2026-02-09T01:15:45","slug":"announcing-typescript-4-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/announcing-typescript-4-2\/","title":{"rendered":"Announcing TypeScript 4.2"},"content":{"rendered":"<p>Today we&#8217;re excited to announce the release of TypeScript 4.2!<\/p>\n<p>For those who aren&#8217;t familiar with TypeScript, it&#8217;s an extension to JavaScript that adds static types and type-checking.\nWith types, you can state exactly what your functions take, and what they&#8217;ll return.\nYou can then use the TypeScript type-checker to catch lots of common mistakes like typos, forgetting to handle <code>null<\/code> and <code>undefined<\/code>, and more.\nBecause TypeScript code just looks like JavaScript with types, everything you know about JavaScript still applies.\nWhen you need, your types can be stripped out, leaving you with clean, readable, runnable JavaScript that works anywhere.\nTo learn more about TypeScript, you can <a href=\"https:\/\/typescriptlang.org\/\">visit our website<\/a>.<\/p>\n<p>To get started using TypeScript 4.2, you can get it <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TypeScript.MSBuild\">through NuGet<\/a>, or use npm with the following command:<\/p>\n<pre class=\"prettyprint language-sh\" style=\"padding: 10px; border-radius: 10px;\"><code>npm install typescript\r\n<\/code><\/pre>\n<p>You can also get editor support by<\/p>\n<ul>\n<li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=TypeScriptTeam.TypeScript-42\">Downloading for Visual Studio 2019\/2017<\/a><\/li>\n<li><a href=\"http:\/\/code.visualstudio.com\/insiders\">Installing the Insiders Version of Visual Studio Code<\/a> or following directions to <a href=\"https:\/\/code.visualstudio.com\/docs\/typescript\/typescript-compiling#_using-newer-typescript-versions\">use a newer version of TypeScript<\/a><\/li>\n<\/ul>\n<ul>\n<li><a href=\"https:\/\/packagecontrol.io\/packages\/TypeScript\">Sublime Text 3 via Package Control<\/a>.<\/li>\n<\/ul>\n<p>Let&#8217;s take a look at what&#8217;s in store for TypeScript 4.2!<\/p>\n<h2 id=\"smarter-type-alias-preservation\">Smarter Type Alias Preservation<\/h2>\n<p>TypeScript has a way to declare new names for types called type aliases.\nIf you&#8217;re writing a set of functions that all work on <code>string | number | boolean<\/code>, you can write a type alias to avoid repeating yourself over and over again.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>type BasicPrimitive = number | string | boolean;\r\n<\/code><\/pre>\n<p>TypeScript has always used a set of rules and guesses for when to reuse type aliases when printing out types.\nFor example, take the following code snippet.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>export type BasicPrimitive = number | string | boolean;\r\n\r\nexport function doStuff(value: BasicPrimitive) {\r\n    let x = value;\r\n    return x;\r\n}\r\n<\/code><\/pre>\n<p>If we hover our mouse over <code>x<\/code> in an editor like Visual Studio, Visual Studio Code, or <a href=\"https:\/\/www.typescriptlang.org\/play?ts=4.1.3#code\/KYDwDg9gTgLgBDAnmYcBCBDAzgSwMYAKUOAtjjDgG6oC8cAdgK4kBGwUcAPnFjMfQHMucFhAgAbYBnoBuAFBzQkWHABmjengoR6cACYQAyjEarVACkoZxjYAC502fEVLkqwAJRwA3nLj+4SXgQODorG2B5ALgoYBMoXRB5AF8gA\">the TypeScript Playground<\/a>, we&#8217;ll get a quick info panel that shows the type <code>BasicPrimitive<\/code>.\nLikewise, if we get the declaration file output (<code>.d.ts<\/code> output) for this file, TypeScript will say that <code>doStuff<\/code> returns <code>BasicPrimitive<\/code>.<\/p>\n<p>However, what happens if we return a <code>BasicPrimitive<\/code> or <code>undefined<\/code>?<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>export type BasicPrimitive = number | string | boolean;\r\n\r\nexport function doStuff(value: BasicPrimitive) {\r\n    if (Math.random() &lt; 0.5) {\r\n        return undefined;\r\n    }\r\n\r\n    return value;\r\n}\r\n<\/code><\/pre>\n<p>We can see what happens <a href=\"https:\/\/www.typescriptlang.org\/play?ts=4.1.3#code\/KYDwDg9gTgLgBDAnmYcBCBDAzgSwMYAKUOAtjjDgG6oC8cAdgK4kBGwUcAPnFjMfQHMucFhAgAbYBnoBuALAAoRQHplcABIRqHCPTgByACYQAyjEYAzC-pHBxEAO4IIPYKgcALDPAAqyYCZ4xGDwhjhYYOIYiFhwFtAIHqhQwOZQekgoAHQqagDqqGQCHvBe1HCgKHgwwIZw5M5wYPzw2Lm5cJ2YuITEZBTl3Iz0hsAWOPS1HR0sjPBs9k5+KIHB8AAsWQBMADT18BO8UnVhEVExcG0Kqh2dTKzswrz8QtyiElJ6QyNjE1PXykUlWg8Asw2qOF0cGMZksFgAFJQMOJGMAAFzobD4IikchUYAASjgAG9FJ1yTgLHB4QBZbweLJQaTGEjwokAHjgAAYsgBWImkhTk4WdFJpPTDUbjSaGeRC4UAX0UZOFYsY6TgSJRwDlcAVQA\">in the TypeScript playground<\/a>.\nWhile we might want TypeScript to display the return type of <code>doStuff<\/code> as <code>BasicPrimitive | undefined<\/code>, it instead displays <code>string | number | boolean | undefined<\/code>!\nWhat gives?<\/p>\n<p>Well this has to do with how TypeScript represents types internally.\nWhen creating a union type out of one or more union types, it will always <em>normalize<\/em> those types into a new flattened union type &#8211; but doing that loses information.\nThe type-checker would have to find every combination of types from <code>string | number | boolean | undefined<\/code> to see what type aliases could have been used, and even then, there might be multiple type aliases to <code>string | number | boolean<\/code>.<\/p>\n<p>In TypeScript 4.2, our internals are a little smarter.\nWe keep track of how types were constructed by keeping around parts of how they were originally written and constructed over time.\nWe also keep track of, and differentiate, type aliases to instances of other aliases!<\/p>\n<p>Being able to print back the types based on how you used them in your code means that as a TypeScript user, you can avoid some unfortunately humongous types getting displayed, and that often translates to getting better <code>.d.ts<\/code> file output, error messages, and in-editor type displays in quick info and signature help.\nThis can help TypeScript feel a little bit more approachable for newcomers.<\/p>\n<p>For more information, check out <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/42149\">the first pull request that improves various cases around preserving union type aliases<\/a>, along with <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/42284\">a second pull request that preserves indirect aliases<\/a>.<\/p>\n<h2 id=\"leadingmiddle-rest-elements-in-tuple-types\">Leading\/Middle Rest Elements in Tuple Types<\/h2>\n<p>In TypeScript, tuple types are meant to model arrays with specific lengths and element types.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\/\/ A tuple that stores a pair of numbers\r\nlet a: [number, number] = [1, 2];\r\n\r\n\/\/ A tuple that stores a string, a number, and a boolean\r\nlet b: [string, number, boolean] = [\"hello\", 42, true];\r\n<\/code><\/pre>\n<p>Over time, TypeScript&#8217;s tuple types have become more and more sophisticated, since they&#8217;re also used to model things like parameter lists in JavaScript.\nAs a result, they can have optional elements and rest elements, and can even have labels for tooling and readability.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\/\/ A tuple that has either one or two strings.\r\nlet c: [string, string?] = [\"hello\"];\r\nc = [\"hello\", \"world\"];\r\n\r\n\/\/ A labeled tuple that has either one or two strings.\r\nlet d: [first: string, second?: string] = [\"hello\"];\r\nd = [\"hello\", \"world\"];\r\n\r\n\/\/ A tuple with a *rest element* - holds at least 2 strings at the front,\r\n\/\/ and any number of booleans at the back.\r\nlet e: [string, string, ...boolean[]];\r\n\r\ne = [\"hello\", \"world\"];\r\ne = [\"hello\", \"world\", false];\r\ne = [\"hello\", \"world\", true, false, true];\r\n<\/code><\/pre>\n<p>In TypeScript 4.2, rest elements specifically been expanded in how they can be used.\nIn prior versions, TypeScript only allowed <code>...rest<\/code> elements at the very last position of a tuple type.<\/p>\n<p>However, now rest elements can occur <em>anywhere<\/em> within a tuple &#8211; with only a few restrictions.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>let foo: [...string[], number];\r\n\r\nfoo = [123];\r\nfoo = [\"hello\", 123];\r\nfoo = [\"hello!\", \"hello!\", \"hello!\", 123];\r\n\r\nlet bar: [boolean, ...string[], boolean];\r\n\r\nbar = [true, false];\r\nbar = [true, \"some text\", false];\r\nbar = [true, \"some\", \"separated\", \"text\", false];\r\n<\/code><\/pre>\n<p>The only restriction is that a rest element can be placed anywhere in a tuple, so long as it&#8217;s not followed by another optional element or rest element.\nIn other words, only one rest element per tuple, and no optional elements after rest elements.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>interface Clown { \/*...*\/ }\r\ninterface Joker { \/*...*\/ }\r\n\r\nlet StealersWheel: [...Clown[], \"me\", ...Joker[]];\r\n\/\/                                    ~~~~~~~~~~ Error!\r\n\/\/ A rest element cannot follow another rest element.\r\n\r\nlet StringsAndMaybeBoolean: [...string[], boolean?];\r\n\/\/                                        ~~~~~~~~ Error!\r\n\/\/ An optional element cannot follow a rest element.\r\n<\/code><\/pre>\n<p>These non-trailing rest elements can be used to model functions that take any number of leading arguments, followed by a few fixed ones.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>declare function doStuff(...args: [...names: string[], shouldCapitalize: boolean]): void;\r\n\r\ndoStuff(\/*shouldCapitalize:*\/ false)\r\ndoStuff(\"fee\", \"fi\", \"fo\", \"fum\", \/*shouldCapitalize:*\/ true);\r\n<\/code><\/pre>\n<p>Even though JavaScript doesn&#8217;t have any syntax to model leading rest parameters, we were still able to declare <code>doStuff<\/code> as a function that takes leading arguments by declaring the <code>...args<\/code> rest parameter with <em>a tuple type that uses a leading rest element<\/em>.\nThis can help model lots of existing JavaScript out there!<\/p>\n<p>For more details, <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41544\">see the original pull request<\/a>.<\/p>\n<h2 id=\"stricter-checks-for-the-in-operator\">Stricter Checks For The <code>in<\/code> Operator<\/h2>\n<p>In JavaScript, it is a runtime error to use a non-object type on the right side of the <code>in<\/code> operator.\nTypeScript 4.2 ensures this can be caught at design-time.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\"foo\" in 42\r\n\/\/       ~~\r\n\/\/ error! The right-hand side of an 'in' expression must not be a primitive.\r\n<\/code><\/pre>\n<p>This check is fairly conservative for the most part, so if you have received an error about this, it is likely an issue in the code.<\/p>\n<p>A big thanks to our external contributor <a href=\"https:\/\/github.com\/jonhue\">Jonas H\u00fcbotter<\/a> for <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41928\">their pull request<\/a>!<\/p>\n<h2 id=\"nopropertyaccessfromindexsignature\"><code>--noPropertyAccessFromIndexSignature<\/code><\/h2>\n<p>Back when TypeScript first introduced index signatures, you could only get properties declared by them with &#8220;bracketed&#8221; element access syntax like <code>person[\"name\"]<\/code>.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>interface SomeType {\r\n    \/** This is an index signature. *\/\r\n    [propName: string]: any;\r\n}\r\n\r\nfunction doStuff(value: SomeType) {\r\n    let x = value[\"someProperty\"];\r\n}\r\n<\/code><\/pre>\n<p>This ended up being cumbersome in situations where we need to work with objects that have arbitrary properties.\nFor example, imagine an API where it&#8217;s common to misspell a property name by adding an extra <code>s<\/code> character at the end.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>interface Options {\r\n    \/** File patterns to be excluded. *\/\r\n    exclude?: string[];\r\n\r\n    \/**\r\n     * It handles any extra properties that we haven't declared as type 'any'.\r\n     *\/\r\n    [x: string]: any;\r\n}\r\n\r\nfunction processOptions(opts: Options) {\r\n    \/\/ Notice we're *intentionally* accessing `excludes`, not `exclude`\r\n    if (opts.excludes) {\r\n        console.error(\"The option `excludes` is not valid. Did you mean `exclude`?\");\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>To make these types of situations easier, a while back, TypeScript made it possible to use &#8220;dotted&#8221; property access syntax like <code>person.name<\/code> when a type had a string index signature.\nThis also made it easier to transition existing JavaScript code over to TypeScript.<\/p>\n<p>However, loosening the restriction also meant that misspelling an explicitly declared property became much easier.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>function processOptions(opts: Options) {\r\n    \/\/ ...\r\n\r\n    \/\/ Notice we're *accidentally* accessing `excludes` this time.\r\n    \/\/ Oops! Totally valid.\r\n    for (const excludePattern of opts.excludes) {\r\n        \/\/ ...\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>In some cases, users would prefer to explicitly opt into the index signature &#8211; they would prefer to get an error message when a dotted property access doesn&#8217;t correspond to a specific property declaration.<\/p>\n<p>That&#8217;s why TypeScript introduces a new flag called <code>--noPropertyAccessFromIndexSignature<\/code>.\nUnder this mode, you&#8217;ll be opted in to TypeScript&#8217;s older behavior that issues an error.\nThis new setting is not under the <code>strict<\/code> family of flags, since we believe users will find it more useful on certain codebases than others.<\/p>\n<p>You can understand this feature in more detail by reading up on the corresponding <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/40171\/\">pull request<\/a>.\nWe&#8217;d also like to extend a big thanks to <a href=\"https:\/\/github.com\/Kingwl\">Wenlu Wang<\/a> who sent us this pull request!<\/p>\n<h2 id=\"abstract-construct-signatures\"><code>abstract<\/code> Construct Signatures<\/h2>\n<p>TypeScript allows us to mark a class as <em>abstract<\/em>.\nThis tells TypeScript that the class is only meant to be extended from, and that certain members need to be filled in by any subclass to actually create an instance.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>abstract class Shape {\r\n    abstract getArea(): number;\r\n}\r\n\r\n\/\/ Error! Can't instantiate an abstract class.\r\nnew Shape();\r\n\r\nclass Square extends Shape {\r\n    #sideLength: number;\r\n\r\n    constructor(sideLength: number) {\r\n        this.#sideLength = sideLength;\r\n    }\r\n\r\n    getArea() {\r\n        return this.#sideLength ** 2;\r\n    }\r\n}\r\n\r\n\/\/ Works fine.\r\nnew Square(42);\r\n<\/code><\/pre>\n<p>To make sure this restriction in <code>new<\/code>-ing up <code>abstract<\/code> classes is consistently applied, you can&#8217;t assign an <code>abstract<\/code> class to anything that expects a construct signature.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>interface HasArea {\r\n    getArea(): number;\r\n}\r\n\r\n\/\/ Error! Cannot assign an abstract constructor type to a non-abstract constructor type.\r\nlet Ctor: new () =&gt; HasArea = Shape;\r\n<\/code><\/pre>\n<p>This does the right thing in case we intend to run code like <code>new Ctor<\/code>, but it&#8217;s overly-restrictive in case we want to write a subclass of <code>Ctor<\/code>.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>functon makeSubclassWithArea(Ctor: new () =&gt; HasArea) {\r\n    return class extends Ctor {\r\n        getArea() {\r\n            \/\/ ...\r\n        }\r\n    }\r\n}\r\n\r\nlet MyShape = makeSubclassWithArea(Shape);\r\n<\/code><\/pre>\n<p>It also doesn&#8217;t work well with built-in helper types like <code>InstanceType<\/code>.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\/\/ Error!\r\n\/\/ Type 'typeof Shape' does not satisfy the constraint 'new (...args: any) =&gt; any'.\r\n\/\/   Cannot assign an abstract constructor type to a non-abstract constructor type.\r\ntype MyInstance = InstanceType&lt;typeof Shape&gt;;\r\n<\/code><\/pre>\n<p>That&#8217;s why TypeScript 4.2 allows you to specify an <code>abstract<\/code> modifier on constructor signatures.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>interface HasArea {\r\n    getArea(): number;\r\n}\r\n\r\n\/\/ Works!\r\nlet Ctor: abstract new () =&gt; HasArea = Shape;\r\n\/\/        ^^^^^^^^\r\n<\/code><\/pre>\n<p>Adding the <code>abstract<\/code> modifier to a construct signature signals that you can pass in <code>abstract<\/code> constructors.\nIt doesn&#8217;t stop you from passing in other classes\/constructor functions that are &#8220;concrete&#8221; &#8211; it really just signals that there&#8217;s no intent to run the constructor directly, so it&#8217;s safe to pass in either class type.<\/p>\n<p>This feature allows us to write <em>mixin factories<\/em> in a way that supports abstract classes.\nFor example, in the following code snippet, we&#8217;re able to use the mixin function <code>withStyles<\/code> with the <code>abstract<\/code> class <code>SuperClass<\/code>.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>abstract class SuperClass {\r\n    abstract someMethod(): void;\r\n    badda() {}\r\n}\r\n\r\ntype AbstractConstructor&lt;T&gt; = abstract new (...args: any[]) =&gt; T\r\n\r\nfunction withStyles&lt;T extends AbstractConstructor&lt;object&gt;&gt;(Ctor: T) {\r\n    abstract class StyledClass extends Ctor {\r\n        getStyles() {\r\n            \/\/ ...\r\n        }\r\n    }\r\n    return StyledClass;\r\n}\r\n\r\nclass SubClass extends withStyles(SuperClass) {\r\n    someMethod() {\r\n        this.someMethod()\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>Note that <code>withStyles<\/code> is demonstrating a specific rule, where a class (like <code>StyledClass<\/code>) that extends a value that&#8217;s generic and bounded by an abstract constructor (like <code>Ctor<\/code>) has to also be declared <code>abstract<\/code>.\nThis is because there&#8217;s no way to know if a class with <em>more<\/em> abstract members was passed in, and so it&#8217;s impossible to know whether the subclass implements all the abstract members.<\/p>\n<p>You can read up more on abstract construct signatures <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/36392\">on its pull request<\/a>.<\/p>\n<h2 id=\"understanding-your-project-structure-with---explainfiles\">Understanding Your Project Structure With <code>--explainFiles<\/code><\/h2>\n<p>A surprisingly common scenario for TypeScript users is to ask &#8220;why is TypeScript including this file?&#8221;.\nInferring the files of your program turns out to be a complicated process, and so there are lots of reasons why a specific combination of <code>lib.d.ts<\/code> got used, why certain files in <code>node_modules<\/code> are getting included, and why certain files are being included even though we thought specifying <code>exclude<\/code> would keep them out.<\/p>\n<p>That&#8217;s why TypeScript now provides an <code>--explainFiles<\/code> flag.<\/p>\n<pre class=\"prettyprint language-sh\" style=\"padding: 10px; border-radius: 10px;\"><code>tsc --explainFiles\r\n<\/code><\/pre>\n<p>When using this option, the TypeScript compiler will give some very verbose output about why a file ended up in your program.\nTo read it more easily, you can forward the output to a file, or pipe it to a program that can easily view it.<\/p>\n<pre class=\"prettyprint language-sh\" style=\"padding: 10px; border-radius: 10px;\"><code># Forward output to a text file\r\ntsc --explainFiles &gt; expanation.txt\r\n\r\n# Pipe output to a utility program like `less`, or an editor like VS Code\r\ntsc --explainFiles | less\r\n\r\ntsc --explainFiles | code -\r\n<\/code><\/pre>\n<p>Typically, the output will start out by listing out reasons for including <code>lib.d.ts<\/code> files, then for local files, and then <code>node_modules<\/code> files.<\/p>\n<pre class=\"language-text\" style=\"padding: 10px; border-radius: 10px;\"><code>TS_Compiler_Directory\/4.2.2\/lib\/lib.es5.d.ts\r\n  Library referenced via 'es5' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.es2015.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.es2015.d.ts\r\n  Library referenced via 'es2015' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.es2016.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.es2016.d.ts\r\n  Library referenced via 'es2016' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.es2017.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.es2017.d.ts\r\n  Library referenced via 'es2017' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.es2018.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.es2018.d.ts\r\n  Library referenced via 'es2018' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.es2019.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.es2019.d.ts\r\n  Library referenced via 'es2019' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.es2020.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.es2020.d.ts\r\n  Library referenced via 'es2020' from file 'TS_Compiler_Directory\/4.2.2\/lib\/lib.esnext.d.ts'\r\nTS_Compiler_Directory\/4.2.2\/lib\/lib.esnext.d.ts\r\n  Library 'lib.esnext.d.ts' specified in compilerOptions\r\n\r\n... More Library References...\r\n\r\nfoo.ts\r\n  Matched by include pattern '**\/*' in 'tsconfig.json'\r\n<\/code><\/pre>\n<p>Right now, we make no guarantees about the output format &#8211; it might change over time.\nOn that note, we&#8217;re interested in improving this format if you have any suggestions!<\/p>\n<p>For more information, <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/40011\">check out the original pull request<\/a>!<\/p>\n<h2 id=\"improved-uncalled-function-checks-in-logical-expressions\">Improved Uncalled Function Checks in Logical Expressions<\/h2>\n<p>Thanks to further improvements from <a href=\"https:\/\/github.com\/a-tarasyuk\">Alex Tarasyuk<\/a>, TypeScript&#8217;s uncalled function checks now apply within <code>&amp;&amp;<\/code> and <code>||<\/code> expressions.<\/p>\n<p>Under <code>--strictNullChecks<\/code>, the following code will now error.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>function shouldDisplayElement(element: Element) {\r\n    \/\/ ...\r\n    return true;\r\n}\r\n\r\nfunction getVisibleItems(elements: Element[]) {\r\n    return elements.filter(e =&gt; shouldDisplayElement &amp;&amp; e.children.length)\r\n    \/\/                          ~~~~~~~~~~~~~~~~~~~~\r\n    \/\/ This condition will always return true since the function is always defined.\r\n    \/\/ Did you mean to call it instead.\r\n}\r\n<\/code><\/pre>\n<p>For more details, <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/issues\/40197\">check out the pull request here<\/a>.<\/p>\n<h2 id=\"destructured-variables-can-be-explicitly-marked-as-unused\">Destructured Variables Can Be Explicitly Marked as Unused<\/h2>\n<p>Thanks to another pull request from <a href=\"https:\/\/github.com\/a-tarasyuk\">Alex Tarasyuk<\/a>, you can now mark destructured variables as unused by prefixing them with an underscore (the <code>_<\/code> character).<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>let [_first, second] = getValues();\r\n<\/code><\/pre>\n<p>Previously, if <code>_first<\/code> was never used later on, TypeScript would issue an error under <code>noUnusedLocals<\/code>.\nNow, TypeScript will recognize that <code>_first<\/code> was intentionally named with an underscore because there was no intent to use it.<\/p>\n<p>For more details, take a look at <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41378\">the full change<\/a>.<\/p>\n<h2 id=\"relaxed-rules-between-optional-properties-and-string-index-signatures\">Relaxed Rules Between Optional Properties and String Index Signatures<\/h2>\n<p>String index signatures are a way of typing dictionary-like objects, where you want to allow access with arbitrary keys:<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>const movieWatchCount: { [key: string]: number } = {};\r\n\r\nfunction watchMovie(title: string) {\r\n  movieWatchCount[title] = (movieWatchCount[title] ?? 0) + 1;\r\n}\r\n<\/code><\/pre>\n<p>Of course, for any movie title not yet in the dictionary, <code>movieWatchCount[title]<\/code> will be <code>undefined<\/code> (TypeScript 4.1 added the option <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/release-notes\/typescript-4-1.html#checked-indexed-accesses---nouncheckedindexedaccess\"><code>--noUncheckedIndexedAccess<\/code><\/a> to include <code>undefined<\/code> when reading from an index signature like this).\nEven though it&#8217;s clear that there must be some strings not present in <code>movieWatchCount<\/code>, previous versions of TypeScript treated optional object properties as unassignable to otherwise compatible index signatures, due to the presence of <code>undefined<\/code>.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>type WesAndersonWatchCount = {\r\n  \"Fantastic Mr. Fox\"?: number;\r\n  \"The Royal Tenenbaums\"?: number;\r\n  \"Moonrise Kingdom\"?: number;\r\n  \"The Grand Budapest Hotel\"?: number;\r\n};\r\n\r\ndeclare const wesAndersonWatchCount: WesAndersonWatchCount;\r\nconst movieWatchCount: { [key: string]: number } = wesAndersonWatchCount;\r\n\/\/    ~~~~~~~~~~~~~~~ error!\r\n\/\/ Type 'WesAndersonWatchCount' is not assignable to type '{ [key: string]: number; }'.\r\n\/\/    Property '\"Fantastic Mr. Fox\"' is incompatible with index signature.\r\n\/\/      Type 'number | undefined' is not assignable to type 'number'.\r\n\/\/        Type 'undefined' is not assignable to type 'number'. (2322)\r\n<\/code><\/pre>\n<p>TypeScript 4.2 allows this assignment. However, it does <em>not<\/em> allow the assignment of non-optional properties with <code>undefined<\/code> in their types, nor does it allow writing <code>undefined<\/code> to a specific key:<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>type BatmanWatchCount = {\r\n  \"Batman Begins\": number | undefined;\r\n  \"The Dark Knight\": number | undefined;\r\n  \"The Dark Knight Rises\": number | undefined;\r\n};\r\n\r\ndeclare const batmanWatchCount: BatmanWatchCount;\r\n\r\n\/\/ Still an error in TypeScript 4.2.\r\n\/\/ `undefined` is only ignored when properties are marked optional.\r\nconst movieWatchCount: { [key: string]: number } = batmanWatchCount;\r\n\r\n\/\/ Still an error in TypeScript 4.2.\r\n\/\/ Index signatures don't implicitly allow explicit `undefined`.\r\nmovieWatchCount[\"It's the Great Pumpkin, Charlie Brown\"] = undefined;\r\n<\/code><\/pre>\n<p>The new rule also does not apply to number index signatures, since they are assumed to be array-like and dense:<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>declare let sortOfArrayish: { [key: number]: string };\r\ndeclare let numberKeys: { 42?: string };\r\n\r\n\/\/ Error! Type '{ 42?: string | undefined; }' is not assignable to type '{ [key: number]: string; }'.\r\nsortOfArrayish = numberKeys;\r\n<\/code><\/pre>\n<p>You can get a better sense of this change <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41921\">by reading up on the original PR<\/a>.<\/p>\n<h2 id=\"declare-missing-helper-function\">Declare Missing Helper Function<\/h2>\n<p>Thanks to <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41215\">a community pull request<\/a> from <a href=\"https:\/\/github.com\/a-tarasyuk\">Alexander Tarasyuk<\/a>, we now have a quick fix for declaring new functions and methods based on the call-site!<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/typescript\/wp-content\/uploads\/sites\/11\/2021\/01\/addMissingFunction-4.2.gif\" alt=\"An un-declared function being called, with a quick fix scaffolding out the new contents of the file\" \/><\/p>\n<h2 id=\"breaking-changes\">Breaking Changes<\/h2>\n<p>We always strive to minimize breaking changes in a release.\nTypeScript 4.2 contains some breaking changes, but we believe they should be manageable in an upgrade.<\/p>\n<h3 id=\"libdts-updates\"><code>lib.d.ts<\/code> Updates<\/h3>\n<p>As with every TypeScript version, declarations for <code>lib.d.ts<\/code> (especially the declarations generated for web contexts), have changed.\nThere are various changes, though <code>Intl<\/code> and <code>ResizeObserver<\/code>&#8216;s may end up being the most disruptive.<\/p>\n<h3 id=\"noimplicitany-errors-apply-to-loose-yield-expressions\"><code>noImplicitAny<\/code> Errors Apply to Loose <code>yield<\/code> Expressions<\/h3>\n<p>When the value of a <code>yield<\/code> expression is captured, but TypeScript can&#8217;t immediately figure out what type you intend for it to receive (i.e. the <code>yield<\/code> expression isn&#8217;t contextually typed), TypeScript will now issue an implicit <code>any<\/code> error.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>function* g1() {\r\n  const value = yield 1;\r\n  \/\/            ~~~~~~~\r\n  \/\/ Error!\r\n  \/\/ 'yield' expression implicitly results in an 'any' type\r\n  \/\/ because its containing generator lacks a return-type annotation.\r\n}\r\n\r\nfunction* g2() {\r\n  \/\/ No error.\r\n  \/\/ The result of `yield 1` is unused.\r\n  yield 1;\r\n}\r\n\r\nfunction* g3() {\r\n  \/\/ No error.\r\n  \/\/ `yield 1` is contextually typed by 'string'.\r\n  const value: string = yield 1;\r\n}\r\n\r\nfunction* g3(): Generator&lt;number, void, string&gt; {\r\n  \/\/ No error.\r\n  \/\/ TypeScript can figure out the type of `yield 1`\r\n  \/\/ from the explicit return type of `g3`.\r\n  const value = yield 1;\r\n}\r\n<\/code><\/pre>\n<p>See more details in <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41348\">the corresponding changes<\/a>.<\/p>\n<h3 id=\"expanded-uncalled-function-checks\">Expanded Uncalled Function Checks<\/h3>\n<p>As described above, uncalled function checks will now operate consistently within <code>&amp;&amp;<\/code> and <code>||<\/code> expressions when using <code>--strictNullChecks<\/code>.\nThis can be a source of new breaks, but is typically an indication of a logic error in existing code.<\/p>\n<h3 id=\"type-arguments-in-javascript-are-not-parsed-as-type-arguments\">Type Arguments in JavaScript Are Not Parsed as Type Arguments<\/h3>\n<p>Type arguments were already not allowed in JavaScript, but in TypeScript 4.2, the parser will parse them in a more spec-compliant way.\nSo when writing the following code in a JavaScript file:<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>f&lt;T&gt;(100)\r\n<\/code><\/pre>\n<p>TypeScript will parse it as the following JavaScript:<\/p>\n<pre class=\"prettyprint language-javascript\" style=\"padding: 10px; border-radius: 10px;\"><code>(f &lt; T) &gt; (100)\r\n<\/code><\/pre>\n<p>This may impact you if you were leveraging TypeScript&#8217;s API to parse type constructs in JavaScript files, which may have occurred when trying to parse Flow files.<\/p>\n<h3 id=\"the-in-operator-no-longer-allows-primitive-types-on-the-right-side\">The <code>in<\/code> Operator No Longer Allows Primitive Types on the Right Side<\/h3>\n<p>As mentioned, it is an error to use a primitive on the right side of an <code>in<\/code> operator, and TypeScript 4.2 is stricter about this sort of code.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\"foo\" in 42\r\n\/\/       ~~\r\n\/\/ error! The right-hand side of an 'in' expression must not be a primitive.\r\n<\/code><\/pre>\n<p>See <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/41928\">the pull request<\/a> for more details on what&#8217;s checked.<\/p>\n<h3 id=\"tuple-size-limits-for-spreads\">Tuple size limits for spreads<\/h3>\n<p>Tuple types can be made by using any sort of spread syntax (<code>...<\/code>) in TypeScript.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\/\/ Tuple types with spread elements\r\ntype NumStr = [number, string];\r\ntype NumStrNumStr = [...NumStr, ...NumStr];\r\n\r\n\/\/ Array spread expressions\r\nconst numStr = [123, \"hello\"] as const;\r\nconst numStrNumStr = [...numStr, ...numStr] as const;\r\n<\/code><\/pre>\n<p>Sometimes these tuple types can accidentally grow to be huge, and that can make type-checking take a long time.\nInstead of letting the type-checking process hang (which is especially bad in editor scenarios), TypeScript has a limiter in place to avoid doing all that work.<\/p>\n<p>You can <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/42448\">see this pull request<\/a> for more details.<\/p>\n<h3 id=\"dts-extensions-cannot-be-used-in-import-paths\"><code>.d.ts<\/code> Extensions Cannot Be Used In Import Paths<\/h3>\n<p>In TypeScript 4.2, it is now an error for your import paths to contain <code>.d.ts<\/code> in the extension.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\/\/ must be changed something like\r\n\/\/   - \".\/foo\"\r\n\/\/   - \".\/foo.js\"\r\nimport { Foo } from \".\/foo.d.ts\";\r\n<\/code><\/pre>\n<p>Instead, your import paths should reflect whatever your loader will do at runtime.\nAny of the following imports might be usable instead.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>import { Foo } from \".\/foo\";\r\nimport { Foo } from \".\/foo.js\";\r\nimport { Foo } from \".\/foo\/index.js\";\r\n<\/code><\/pre>\n<h3 id=\"reverting-template-literal-inference\">Reverting Template Literal Inference<\/h3>\n<p>This change removed a feature from TypeScript 4.2 beta.\nIf you haven&#8217;t yet upgraded past our last stable release, you won&#8217;t be affected, but you may still be interested in the change.<\/p>\n<p>The beta version of TypeScript 4.2 included a change in inference to template strings.\nIn this change, template string literals would either be given template string types or simplify to multiple string literal types.\nThese types would then <em>widen<\/em> to <code>string<\/code> when assigning to mutable variables.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>declare const yourName: string;\r\n\r\n\/\/ 'bar' is constant.\r\n\/\/ It has type '`hello ${string}`'.\r\nconst bar = `hello ${yourName}`;\r\n\r\n\r\n\/\/ 'baz' is mutable.\r\n\/\/ It has type 'string'.\r\nlet baz = `hello ${yourName}`;\r\n<\/code><\/pre>\n<p>This is similar to how string literal inference works.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>\/\/ 'bar' has type '\"hello\"'.\r\nconst bar = \"hello\";\r\n\r\n\/\/ 'baz' has type 'string'.\r\nlet baz = \"hello\";\r\n<\/code><\/pre>\n<p>For that reason, we believed that making template string expressions have template string types would be &#8220;consistent&#8221;;\nhowever, from what we&#8217;ve seen and heard, that isn&#8217;t always desirable.<\/p>\n<p>In response, we&#8217;ve reverted this feature (and potential breaking change).\nIf you <em>do<\/em> want a template string expression to be given a literal-like type, you can always add <code>as const<\/code> to the end of it.<\/p>\n<pre class=\"prettyprint language-typescript\" style=\"padding: 10px; border-radius: 10px;\"><code>declare const yourName: string;\r\n\r\n\/\/ 'bar' has type '`hello ${string}`'.\r\nconst bar = `hello ${yourName}` as const;\r\n\/\/                              ^^^^^^^^\r\n\r\n\/\/ 'baz' has type 'string'.\r\nconst baz = `hello ${yourName}`;\r\n<\/code><\/pre>\n<h3 id=\"typescripts-lift-callback-in-visitnode-uses-a-different-type\">TypeScript&#8217;s <code>lift<\/code> Callback in <code>visitNode<\/code> Uses a Different Type<\/h3>\n<p>TypeScript has a <code>visitNode<\/code> function that takes a <code>lift<\/code> function.\n<code>lift<\/code> now expects a <code>readonly Node[]<\/code> instead of a <code>NodeArray&lt;Node&gt;<\/code>.\nThis is technically an API breaking change which you can read more on <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/pull\/42000\">here<\/a>.<\/p>\n<h2 id=\"whats-next\">What&#8217;s Next?<\/h2>\n<p>While 4.2 was just released, our team is already hard at work on TypeScript 4.3. You can <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/issues\/42762\">take a look at the TypeScript 4.3 iteration plan<\/a> and our <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/wiki\/Roadmap\">rolling feature roadmap<\/a> to keep track of what we&#8217;re working on.<\/p>\n<p>You can also stay on the bleeding edge with <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/nightly-builds.html\">TypeScript nightly releases too<\/a>, and with our <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.vscode-typescript-next\">nightly Visual Studio Code extension<\/a>.\nNightly releases tend to be fairly stable, and early feedback is encouraged and appreciated!<\/p>\n<p>But we expect most users will stick with TypeScript 4.2 for now;\nand so if you are using TypeScript 4.2, we hope this release is easy to adopt and makes you a lot more productive.\nIf not, <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/issues\/new\/choose\">we want to hear about it<\/a>!\nWe want to make sure that TypeScript brings you joy in your coding, and we hope we&#8217;ve done exactly that.<\/p>\n<p>Happy Hacking!<\/p>\n<p>&#8211; Daniel Rosenwasser and the TypeScript Team<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we&#8217;re excited to announce the release of TypeScript 4.2! For those who aren&#8217;t familiar with TypeScript, it&#8217;s an extension to JavaScript that adds static types and type-checking. With types, you can state exactly what your functions take, and what they&#8217;ll return. You can then use the TypeScript type-checker to catch lots of common mistakes [&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-2880","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 the release of TypeScript 4.2! For those who aren&#8217;t familiar with TypeScript, it&#8217;s an extension to JavaScript that adds static types and type-checking. With types, you can state exactly what your functions take, and what they&#8217;ll return. You can then use the TypeScript type-checker to catch lots of common mistakes [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/2880","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=2880"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/2880\/revisions"}],"predecessor-version":[{"id":5050,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/2880\/revisions\/5050"}],"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=2880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=2880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=2880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}