title: Destructuring object allows to assign incorrect value to typed variable in TypeScript
body: <p>Let's say there are two interfaces:</p>
  <pre><code>interface S {
    s: string
  }
  
  interface AB {
    a?: S[],
    b?: number
  }
  </code></pre>
  <p>We can create a constant of type S and unassigned variable ab:</p>
  <pre><code>const constS: S = { s: 'const' }
  let ab: AB
  </code></pre>
  <p>Normally we would assign something like this if we wanted to use value of constant <code>constS</code>:</p>
  <pre><code>ab = { a: [constS], b: 1 }
  </code></pre>
  <p>Now the question is why TypeScript allows to assign a value to variable <code>ab</code> that is not of type <code>AB</code> when using destructuring (spread operator) like this:</p>
  <pre><code>ab = { ...constS, b: 1 }
  </code></pre>
  <p>If we log <code>ab</code> now we get: <code>{ s: 'const', b: 1 }</code>, but trying to assign this value to <code>ab</code> Typescript, correctly, throws an exception:</p>
  <pre><code>&gt; ab = { s: 'const', b: 1 }
  error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'AB'.
    Object literal may only specify known properties, and 's' does not exist in type 'AB'.
  </code></pre>
  <p>but the variable even has the value we're trying to assign.</p>
  <p>Side note, trying to assign <code>ab = { ...constS }</code> throws <code>Type '{ s: string; }' has no properties in common with type 'AB'</code>.</p>
  <p>Why TS allows for this behavior and how can I prevent object <code>ab</code> from having properties not in its type?</p>