title: Typescript: ***Compile-time*** validation that two types are equal?
body: <p>I'm looking to have the equivalent of C <code>static_assert()</code>, but only usable against compile-time data, such as type information (which isn't emitted).</p>
  I'd like something similar to the following...
  <pre class="lang-js prettyprint-override"><code>type TStrLit_0 = 'a' | 'b' | 'C' | 'd';
  type TStrLit_1 = 'a' | 'b' | 'C' | 'd';
  type TStrLit_2 = 'a' | 'b' | 'c' | 'd';
  type TStrLit_3 = 'a' | 'b' | 'C'      ;
  type TStrLit_4 = 'a' | 'b' | 'C' |'d' | 'e';
  
  static_assert(  TypesAreEqual&lt;TStrLit_0, TStrLit_1&gt;() );
  static_assert( !TypesAreEqual&lt;TStrLit_0, TStrLit_1&gt;() );
  static_assert( !TypesAreEqual&lt;TStrLit_0, TStrLit_1&gt;() );
  static_assert( !TypesAreEqual&lt;TStrLit_0, TStrLit_1&gt;() );
  static_assert( !TypesAreEqual&lt;TStrLit_0, TStrLit_1&gt;() );
  
  type TObject_0 = { a: number, b: string };
  type TObject_1 = { a: number, b: string };
  type TObject_2 = { a: number, b: number };
  type TObject_3 = { a: number, B: string };
  type TObject_4 = { a: number            };
  type TObject_5 = { a: number, b: string, c: number };
  type TObject_6 = { a: any,    b: any    };
  static_assert( TypesAreEqual&lt;TObject_0, TObject_1&gt;() );
  static_assert(!TypesAreEqual&lt;TObject_0, TObject_2&gt;() );
  static_assert(!TypesAreEqual&lt;TObject_0, TObject_3&gt;() );
  static_assert(!TypesAreEqual&lt;TObject_0, TObject_4&gt;() );
  static_assert(!TypesAreEqual&lt;TObject_0, TObject_5&gt;() );
  static_assert(!TypesAreEqual&lt;TObject_0, TObject_6&gt;() );
  </code></pre>
  
  <p>What I'm looking for is the generic ability to have typescript-specific, compile-time-only assertions based on the type information.</p>
  <p>The compiler already generates the type information, but it's not emitted and thus any runtime assertions have to first change the source to emit extra metadata.</p>
  <p>(e.g., for string literals, generating the type from an <code>as const</code> string array, adding typeguard functions, out-of-band tracking the relationship between those string literal types and the source string arrays, ... ugh!)</p>
  <p>Having compile-time type information assertions would allow embedding assumed invariants into the code, saving future maintainers pain when they unknowingly violate those invariants.</p>
  <p><em><strong>Does anything like this already exist?</strong></em></p>
  <p><em><strong>Would this require changes to the TypeScript core, or can it be done with existing functionality?</strong></em></p>
  <p>Thanks!</p>