<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: yamatai12</title>
    <description>The latest articles on DEV Community by yamatai12 (@taiyama1212).</description>
    <link>https://dev.to/taiyama1212</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1310833%2F7d9e430c-f8d7-4942-afeb-7291b7a6c5e7.jpeg</url>
      <title>DEV Community: yamatai12</title>
      <link>https://dev.to/taiyama1212</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taiyama1212"/>
    <language>en</language>
    <item>
      <title>How to ban type assertions with lint</title>
      <dc:creator>yamatai12</dc:creator>
      <pubDate>Fri, 01 Mar 2024 15:59:43 +0000</pubDate>
      <link>https://dev.to/taiyama1212/encouraging-type-safety-strategies-for-disallowing-type-assertions-in-your-codebase-2cl2</link>
      <guid>https://dev.to/taiyama1212/encouraging-type-safety-strategies-for-disallowing-type-assertions-in-your-codebase-2cl2</guid>
      <description>&lt;h2&gt;
  
  
  1. conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/eslint-plugin-no-type-assertion"&gt;https://www.npmjs.com/package/eslint-plugin-no-type-assertion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They say you can use this one!&lt;/p&gt;

&lt;h2&gt;
  
  
  2. background
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hoge&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will often see statements like this.&lt;br&gt;
Type assertions are type-checked as the type after &lt;code&gt;as&lt;/code&gt; no matter what the value of the variable is.&lt;br&gt;
This can cause errors at runtime.&lt;br&gt;
We have a coding rule in our company that we try not to use them as much as possible.&lt;/p&gt;

&lt;p&gt;So I decided to check if there is such a rule in eslint.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. investigate plugins that prohibit type assertions
&lt;/h2&gt;

&lt;p&gt;I found the following hits&lt;br&gt;
typescript-eslint and tslint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/typescript-eslint/typescript-eslint/issues/1310"&gt;https://github.com/typescript-eslint/typescript-eslint/issues/1310&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1. typescript-eslint
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://typescript-eslint.io/rules/no-unnecessary-type-assertion/"&gt;no-unnecessary-type-assertion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;This rule reports when a type assertion does not change the type of an expression&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This rule reports when a type assertion does not change the type of an expression.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2. TsLint
&lt;/h3&gt;

&lt;p&gt;tslint also had a rule no-unnecessary-type-assertion, but it was deprecated as follows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://palantir.github.io/tslint/"&gt;https://palantir.github.io/tslint/&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ tslint has been deprecated as of 2019.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  3.3. EsLint
&lt;/h3&gt;

&lt;p&gt;It was in plugin.&lt;/p&gt;

&lt;h3&gt;
  
  
  eslint-plugin-no-type-assertion
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/eslint-plugin-no-type-assertion"&gt;https://www.npmjs.com/package/eslint-plugin-no-type-assertion&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disallow type assertions in TypeScript code. the rule will forbid both as operator and the angle-bracketed syntax, unless used for const assertions or with the unknown type&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will do it.&lt;br&gt;
And it's nice to know that const assertions are not an error!&lt;/p&gt;

&lt;h3&gt;
  
  
  ⚠️eslint-plugin-no-type-assertion also forbids non-null assertions!
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The rule also forbids non-null assertions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-"&gt;https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Other
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Type assertions are different from casts.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://typescript-eslint.io/rules/consistent-type-assertions"&gt;https://typescript-eslint.io/rules/consistent-type-assertions&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Type assertions are also commonly referred as "type casting" in TypeScript. Type assertions are a way to say to the TypeScript compiler, "I know better than you, it's actually this different type! !".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Type assertions are also often referred to as casts, but strictly speaking they are not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally.
&lt;/h2&gt;

&lt;p&gt;I would like to leave the coding rules to linter and reduce the burden on reviewers!&lt;/p&gt;

&lt;p&gt;Thanks for looking.&lt;br&gt;
This is my dev.to first post🎉&lt;/p&gt;

</description>
      <category>eslint</category>
      <category>typescript</category>
      <category>typeassertion</category>
    </item>
  </channel>
</rss>
