title: Can TypeScript warn me about superfluous uses of the non-null assertion operator?
body: <p>Consider the following TypeScript code:</p>
  
  <pre><code>let x = { foo: { bar: "baz" } };
  console.log(x.foo!.bar);
  </code></pre>
  
  <p>Here, the inferred type of <code>x.foo</code> is <code>{ bar: string }</code>, and so there is no need for the use of the non-null assertion operator <code>!</code> (see e.g. <a href="https://github.com/Microsoft/TypeScript/issues/7395" rel="noreferrer">https://github.com/Microsoft/TypeScript/issues/7395</a>): the compiler <em>already</em> knows that <code>x.foo</code> cannot be null.</p>
  
  <p>Can tsc (or possibly a widely-used third party tool like TSLint) warn me about these superfluous uses of <code>!</code>, and/or is this a planned feature?</p>
  
  <p>I'm sure this is feasible in principle (seems like this'd just be a matter of using the TypeScript compiler API to identify places where <code>!</code> is used on variables whose type is not <code>T | null</code> or <code>T | undefined</code>); I'm just wondering if the heavy lifting has already been done. Or, I guess, a pointer to the relevant parts of the TypeScript compiler API would work too - it's tough finding documentation for it (I guess because it's not yet stable).</p>
  
  <p>(The reason I ask is that I'm concerned that I'm going to end up having to deal with code where <s>past me</s> some idiot sprinkled <code>!</code> everywhere to make <code>tsc --strictNullChecks</code> stop issuing errors, without actually thinking about whether the asserted variable actually <em>will</em> be non-null at runtime. If it does end up null at runtime, voila! A bug that we could've caught at compile time, but didn't because of overzealous <code>!</code>. The presence of <code>!</code>s on variables already known to be non-null would strongly hint that the code is overusing <code>!</code>, likely incorrectly.)</p>