title: TypeScript: check types compatibility
body: <p>From two representations of TypeScript types, which could be strings like:</p>
  
  <p>A:</p>
  
  <pre><code>{
      a: string
      b: number
  }
  </code></pre>
  
  <p>B:</p>
  
  <pre><code>{
      a: string
  }
  </code></pre>
  
  <p>How can I programmatically test if they are compatible, answering the question "can I assign a variable of type A to a value of type B?" (or vice-versa). </p>
  
  <p>The function would look like this: </p>
  
  <pre><code>function match(source: string, target: string): boolean { /** */ }
  
  match('number', 'any') // true
  match('any', 'number') // false
  match('{a: string; b: number}', '{a: string; b: number}') // true
  match('{a: string; b: number}', '{a: string}') // true
  match('{a: string}', '{a: string; b: number}') // false
  // ...
  </code></pre>
  
  <p>What is the simplest way of doing that?</p>
  
  <p>EDIT: The use case is that I have custom user generated type interfaces and I want to check if they are compatible on design time. TypeScript is just the syntax being used for expressing those types, but this is secondary to the challenge of asserting type matching. It could be any other type system.</p>