{"componentChunkName":"component---gatsby-theme-teklog-src-templates-post-tsx","path":"/typescript-evolution/ts-expect-error","result":{"data":{"site":{"siteMetadata":{"siteUrl":"https://blog.tekmi.nl","author":{"name":"Teklog","avatar":"/img/tekmi-icon.svg"}}},"blogPost":{"frontmatter":{"title":"New Feature: ts-expect-error","slug":"typescript-evolution/ts-expect-error","series":"typescript-evolution","date":"2020-06-25","datePretty":"25 June, 2020","coverImage":{"childImageSharp":{"fixed":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEDBP/EABUBAQEAAAAAAAAAAAAAAAAAAAMC/9oADAMBAAIQAxAAAAFuQR0MQt//xAAbEAEAAgIDAAAAAAAAAAAAAAABAAIDIRESIv/aAAgBAQABBQJ3A1WnmvYl8iHJP//EABURAQEAAAAAAAAAAAAAAAAAAAAB/9oACAEDAQE/AUr/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEv/aAAgBAgEBPwFL/8QAGxAAAQQDAAAAAAAAAAAAAAAAAAECEjERUWH/2gAIAQEABj8C4UUMdLMtioUf/8QAHBAAAwABBQAAAAAAAAAAAAAAAAERQSFhgZHh/9oACAEBAAE/IaReCzrS98iovQhoyITCoWkjk//aAAwDAQACAAMAAAAQIw//xAAYEQACAwAAAAAAAAAAAAAAAAAAEQEhMf/aAAgBAwEBPxBWSrD/xAAYEQACAwAAAAAAAAAAAAAAAAAAEQExQf/aAAgBAgEBPxBOhI0//8QAHBABAQADAAMBAAAAAAAAAAAAAREAITFBUWHw/9oACAEBAAE/EA4Ck47T7hRCWAgPk8YhIO9cG8YgAUVj1jgOvpP3MQcIj7Z//9k=","width":1000,"height":625,"src":"/static/df08abb9694b4c9f0f47370213ec1bf1/a7715/ts_expect_error.jpg","srcSet":"/static/df08abb9694b4c9f0f47370213ec1bf1/a7715/ts_expect_error.jpg 1x"}}},"author":"Adam","authorAvatar":null},"timeToRead":10,"excerpt":"Introduction TypeScript has been evolving steadily since 2012, the year when Microsoft announced its first public preview.  With the TypeScript 3.…","html":"<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>TypeScript has been evolving steadily since 2012, the year when Microsoft announced its first <a href=\"https://devblogs.microsoft.com/typescript/announcing-typescript-0-8-1/\">public preview</a>. </p>\n<p>With the <a href=\"https://devblogs.microsoft.com/typescript/announcing-typescript-3-9/\">TypeScript 3.9</a> version released in May 2020,\na new <code class=\"language-text\">@ts-expect-error</code> directive, among other features, has been made available to TypeScript developers.\nWhile having slight similarities to <code class=\"language-text\">@ts-ignore</code>, <code class=\"language-text\">@ts-expect-error</code> is more sophisticated and does additional code analysis that wasn't possible before.</p>\n<p>If you are interested in knowing more about it, let's evaluate it together.</p>\n<h2 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Prerequisites</h2>\n<ul>\n<li>NodeJS Long Time Support (Version 12.x)</li>\n<li>\n<p>NodeJS Package Manager (Version 6.x)</p>\n<ul>\n<li>Yarn is also good, however, all examples here will use <em>npm</em>.</li>\n</ul>\n</li>\n<li>TypeScript (Versions: 3.9.x and 3.8.x)</li>\n<li>Docker Engine 17.09.0+</li>\n<li>GIT</li>\n</ul>\n<p>These are soft requirements. If you have little time, you don't need to install any of them, nor follow the steps below.\nFeel free to explore the accompanying <a href=\"https://github.com/tekmi/typescript-evolution/tree/master/ts-expect-error\">Github Repository</a> instead\nor jump right to the <a href=\"#allowed-usage-variations\">Allowed Usage Variations Section</a> further down.</p>\n<h2 id=\"local-docker-environment-setup\" style=\"position:relative;\"><a href=\"#local-docker-environment-setup\" aria-label=\"local docker environment setup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Local Docker Environment Setup</h2>\n<p>To get the most out of this tutorial, we recommend setting up the local Docker environment, as outlined below. This will allow you to run multiple TypeScript Compiler's versions at the same time.</p>\n<ol>\n<li>\n<p>Clone the accompanying repository</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">git</span> clone https://github.com/tekmi/typescript-evolution</code></pre></div>\n</li>\n<li>\n<p>Build local Docker images for given TSC versions (each image weighs around 180-200 MB)</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">cd</span> typescript-evolution\ndocker-compose build</code></pre></div>\n</li>\n<li>\n<p>Run local Docker containers based on created <code class=\"language-text\">tekmi/*</code> images to verify the TSC versions</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">docker run --rm tekmi/tsc39 tsc --version\ndocker run --rm tekmi/tsc38 tsc --version\n\n// output at the <span class=\"token function\">time</span> of writing\nVersion <span class=\"token number\">3.9</span>.5\nVersion <span class=\"token number\">3.8</span>.3</code></pre></div>\n</li>\n</ol>\n<p>Having that, we are good to go. Testing <code class=\"language-text\">@ts-expect-error</code> directive with multiple TypeScript Compiler's versions should be a piece of cake now.</p>\n<h2 id=\"examining-ts-expect-error\" style=\"position:relative;\"><a href=\"#examining-ts-expect-error\" aria-label=\"examining ts expect error permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Examining \"@ts-expect-error\"</h2>\n<p>The previously cloned repository comes with a few simple examples, grouped under <strong>ts-expect-error</strong> directory.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">├── Dockerfile\n├── README.md\n├── docker-compose.yml\n└── ts-expect-error\n    ├── helper.ts\n    ├── package.json\n    ├── unused.ts\n    └── used.ts</code></pre></div>\n<p>The <strong>package.json</strong> file is fairly simple and has no packages to install. Instead, it comes with two scripts that alias lengthy docker commands:</p>\n<ul>\n<li><code class=\"language-text\">tsc39</code> that creates the docker container based on the local <code class=\"language-text\">tekmi/tsc39</code> image, including volume sharing and working directory setup</li>\n<li><code class=\"language-text\">tsc38</code> that creates the docker container based on the local <code class=\"language-text\">tekmi/tsc38</code> image, with volume sharing the working directory setup as well</li>\n</ul>\n<p>The <strong>helper.ts</strong> file defines a simple function that accepts two arguments (<em>num1</em> and <em>num2</em>). Each of these arguments must be of type <em>number</em>, as shown below:</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">iCanAddTwoNumbers</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">num1<span class=\"token operator\">:</span> <span class=\"token builtin\">number</span><span class=\"token punctuation\">,</span> num2<span class=\"token operator\">:</span> <span class=\"token builtin\">number</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> num1 <span class=\"token operator\">+</span> num2<span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <strong>used.ts</strong> and <strong>unused.ts</strong> files are the most important to us because we will be exercising the <code class=\"language-text\">@ts-expect-error</code> directive in there.\nLet's start with <strong>used.ts</strong> file and try to understand what is going on there:</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> iCanAddTwoNumbers <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./helper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// @ts-expect-error</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<ul>\n<li>We import the <code class=\"language-text\">iCanAddTwoNumbers</code> function from the <strong>helper.ts</strong> file.</li>\n<li>We pass the <code class=\"language-text\">notAllowed</code> string as the first argument to the <code class=\"language-text\">iCanAddTwoNumbers</code> function which is obviously not allowed.</li>\n<li>We use the <code class=\"language-text\">@ts-expect-error</code> directive in the comments to suppress TypeScript Compiler's errors.</li>\n</ul>\n<p>To prove the TSC 3.9 Compiler is content, being in <code class=\"language-text\">typescript-evolution/ts-expect-error</code> directory, let's run the following command:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> run -s tsc39 used.ts</code></pre></div>\n<p>No errors which mean TSC 3.9 has honored our new <code class=\"language-text\">@ts-expect-error</code> directive. If we run it with TSC 3.8 version though:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> run -s tsc38 used.ts</code></pre></div>\n<p>we will immediately see the following error:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">used.ts<span class=\"token punctuation\">(</span><span class=\"token number\">4,31</span><span class=\"token punctuation\">)</span>: error TS2345: Argument of <span class=\"token builtin class-name\">type</span> <span class=\"token string\">'\"notAllowed\"'</span> is not assignable to parameter of <span class=\"token builtin class-name\">type</span> <span class=\"token string\">'number'</span><span class=\"token builtin class-name\">.</span></code></pre></div>\n<p>This proves that TSC 3.8 is not aware of the new <code class=\"language-text\">@ts-expect-error</code> directive, hence it reacts angrily by raising a compilation error.</p>\n<p>Knowing that, let's continue with our second file, <strong>unused.ts</strong> that contains the following code:</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> iCanAddTwoNumbers <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'./helper'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// @ts-expect-error</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>It is almost the same as the <strong>used.ts</strong> file, the only difference is the type of the first argument passed to the <code class=\"language-text\">iCanAddTwoNumbers</code> function.\nLet's start with running <strong>unused.ts</strong> via TSC 3.8 Compiler as follows:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> run -s tsc38 unused.ts</code></pre></div>\n<p>As expected, TSC 3.8 didn't report any errors. However, if we run it via TSC 3.9 Compiler as shown below:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">npm</span> run -s tsc39 unused.ts</code></pre></div>\n<p>we will notice this new, exotic error:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">unused.ts<span class=\"token punctuation\">(</span><span class=\"token number\">3,1</span><span class=\"token punctuation\">)</span>: error TS2578: Unused <span class=\"token string\">'@ts-expect-error'</span> directive.</code></pre></div>\n<p>This <code class=\"language-text\">TS2578</code> error is a proof of the new suppression mechanism sitting behind the <code class=\"language-text\">@ts-expect-error</code> directive and working properly.\nWe will come back to this later, but for now, let's summarize our findings of TSC 3.8 and TSC 3.9 Compilers in the table below:</p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">TSC</th>\n<th align=\"center\">used.ts</th>\n<th align=\"center\">unused.ts</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">TSC 3.9</td>\n<td align=\"center\">OK</td>\n<td align=\"center\">error TS2578</td>\n</tr>\n<tr>\n<td align=\"left\">TSC 3.8</td>\n<td align=\"center\">error TS2345</td>\n<td align=\"center\">OK</td>\n</tr>\n</tbody>\n</table>\n<p>Note:\nYou could also use the <a href=\"https://www.typescriptlang.org/play/index.html\">TypeScript Playground</a> to perform similar tests.\nHowever, you cannot import any files in there, which means all your code goes into one giant textarea.\nAdditionally, you may not find the exact TypeScript Compiler's version you need because only a few are being emulated there.\nFor example, at the time of writing, the latest TypeScript version is 3.9.5, while the TypeScript Playground supports only the maximum 3.9.2 version (plus the Nightly build).</p>\n<ul>\n<li><a href=\"https://www.typescriptlang.org/play/index.html?ts=3.9.2\">https://www.typescriptlang.org/play/index.html?ts=3.9.2</a></li>\n<li><a href=\"https://www.typescriptlang.org/play/index.html?ts=3.8.3\">https://www.typescriptlang.org/play/index.html?ts=3.8.3</a></li>\n</ul>\n<p>At this point, we know that <code class=\"language-text\">@ts-expect-error</code> should be used inside the comments. Let's tinker with it a bit more and explore a few possible variations together.</p>\n<h2 id=\"allowed-usage-variations\" style=\"position:relative;\"><a href=\"#allowed-usage-variations\" aria-label=\"allowed usage variations permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Allowed Usage Variations</h2>\n<p>TypeScript 3.9 Compiler accepts the following scenarios:</p>\n<ul>\n<li>\n<p>it doesn't mind how many spaces are in front of the <code class=\"language-text\">@ts-expect-error</code> directive</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">//        @ts-expect-error</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>it doesn't care if some spaces precede the comment with <code class=\"language-text\">@ts-expect-error</code> directive</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\">           <span class=\"token comment\">// @ts-expect-error</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>it doesn't bother if there is any text after the directive</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// @ts-expect-error random text here</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>it doesn't mind if empty lines sit in-between</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// @ts-expect-error</span>\n\n\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>it doesn't care if there is another, one-line comment in-between</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// @ts-expect-error</span>\n\n<span class=\"token comment\">// another one-line comment</span>\n\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>it permits one-line comments with <code class=\"language-text\">/* */</code> notation</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">/* @ts-expect-error */</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n</ul>\n<p>Therefore, if you feel adventurous, you could write your code as shown below and TSC 3.9 Compiler would still understand you.</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\">      <span class=\"token comment\">/*      @ts-expect-error some text */</span>\n\n<span class=\"token comment\">// another comment</span>\n\n\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h2 id=\"disallowed-usage-variations\" style=\"position:relative;\"><a href=\"#disallowed-usage-variations\" aria-label=\"disallowed usage variations permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Disallowed Usage Variations</h2>\n<p>Knowing what is allowed, it's time to explore a few use cases where the TSC 3.9 Compiler would protest, such as:</p>\n<ul>\n<li>\n<p>comments with <code class=\"language-text\">/* */</code> syntax that span to multiple lines</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">/* @ts-expect-error\n*/</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>the comment with <code class=\"language-text\">@ts-ignore</code> directive placed after the comment with <code class=\"language-text\">@ts-expect-error</code> directive</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// @ts-expect-error</span>\n\n<span class=\"token comment\">// @ts-ignore</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>the comment with <code class=\"language-text\">/* */</code> syntax sitting in-between</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// @ts-expect-error</span>\n\n<span class=\"token comment\">/* hmm, not welcomed here... */</span>\n\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n<li>\n<p>the <code class=\"language-text\">@ts-expect-error</code> directive put directly in the same line</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// @ts-expect-error</span></code></pre></div>\n</li>\n<li>\n<p>any text placed before the <code class=\"language-text\">@ts-expect-error</code> directive</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// no text before me @ts-expect-error</span>\n<span class=\"token builtin\">console</span><span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">iCanAddTwoNumbers</span><span class=\"token punctuation\">(</span><span class=\"token string\">'notAllowed'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n</li>\n</ul>\n<p>After exploring the allowed and disallowed usage variations, let's move on to the last section that should serve as guidance on when to pick the <code class=\"language-text\">@ts-expect-error</code> directive over the <code class=\"language-text\">@ts-ignore</code> one.</p>\n<h2 id=\"ts-expect-error-or-ts-ignore\" style=\"position:relative;\"><a href=\"#ts-expect-error-or-ts-ignore\" aria-label=\"ts expect error or ts ignore permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>\"@ts-expect-error\" or \"@ts-ignore\"</h2>\n<p>Both <code class=\"language-text\">@ts-expect-error</code> and <code class=\"language-text\">@ts-ignore</code> know how to soothe the TypeScript Compiler and can be used in the comments.</p>\n<p>However, <code class=\"language-text\">@ts-expect-error</code> brings a new suppression mechanism to the table, as we briefly experienced before with the <code class=\"language-text\">TS2578</code> error.\nThanks to this mechanism, the TypeScript Compiler can monitor the correlated line of code and inform us whether the usage of <code class=\"language-text\">@ts-expect-error</code> directive still makes sense or not.</p>\n<ul>\n<li>As soon as this line of code becomes valid and doesn't trouble the compiler anymore, it's time to remove the <code class=\"language-text\">@ts-expect-error</code> directive too.</li>\n<li>At the time of writing, there is no way to define which errors to expect with <code class=\"language-text\">@ts-expect-error</code>, but some people were suggesting it under the original <a href=\"https://github.com/microsoft/TypeScript/pull/36014#issuecomment-581062976\">Pull Request</a></li>\n</ul>\n<p>With that, you may be thinking to switch all <code class=\"language-text\">@ts-ignore</code> directives to <code class=\"language-text\">@ts-expect-error</code>, have a mix of them, or don't bother at all about it yet.\nAs we don't want to bias you with our subjective view, we encourage you to read the recommendations from TypeScript maintainers.</p>\n<ul>\n<li><a href=\"https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#-ts-expect-error-comments\">Handbook - Release Notes - 3.9</a> </li>\n<li><a href=\"https://devblogs.microsoft.com/typescript/announcing-typescript-3-9/#ts-expect-error-comments\">DevBlogs - TypeScript - 3.9</a> </li>\n</ul>\n<p>The table below collects their thoughts and recommendations succinctly.</p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Pick \"ts-expect-error\" if</th>\n<th align=\"left\">Pick \"ts-ignore\" if</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">you’re writing test code where you actually want the type system to error on an operation</td>\n<td align=\"left\">you have an a larger project and and new errors have appeared in code with no clear owner</td>\n</tr>\n<tr>\n<td align=\"left\">you expect a fix to be coming in fairly quickly and you just need a quick workaround</td>\n<td align=\"left\">you are in the middle of an upgrade between two different versions of TypeScript, and a line of code errors in one version but not another</td>\n</tr>\n<tr>\n<td align=\"left\">you’re in a reasonably-sized project with a proactive team that wants to remove suppression comments as soon affected code is valid again</td>\n<td align=\"left\">you honestly don’t have the time to decide which of these options is better</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"wrap-up\" style=\"position:relative;\"><a href=\"#wrap-up\" aria-label=\"wrap up permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Wrap-up</h2>\n<p>In this tutorial, we have looked into the new <code class=\"language-text\">@ts-expect-error</code> addition to the TypeScript 3.9 and covered the following aspects:</p>\n<ul>\n<li>the preparation of the local Docker environment to run multiple versions of TypeScript Compiler</li>\n<li>the comparison of TSC 3.8 and TSC 3.9 behaviors concerning the <code class=\"language-text\">@ts-expect-error</code></li>\n<li>the allowed and disallowed usage variations of <code class=\"language-text\">@ts-expect-error</code></li>\n<li>the guidance on when to pick the <code class=\"language-text\">@ts-expect-error</code> directive over <code class=\"language-text\">@ts-ignore</code></li>\n</ul>\n<p>Thanks to the evolution of TypeScript and its 3.9 release, we have just got another tool under our toolset.\nWith the introduction of <code class=\"language-text\">@ts-expect-error</code>, we are sure you will be able to tackle your existing TypeScript problems with a new and fresh approach.</p>"}},"pageContext":{"postId":"bab03e57-a244-5952-8ff4-348c8ec8edc6","slug":"typescript-evolution/ts-expect-error"}}}