title: Type assertion vs properties
body: <p>I've noticed the following behavior:</p>
  
  <pre><code>interface User {
      name: string;
      age: number;
  }
  
  let user:User = {
      name: "Aaron",
      age: 99
  }
  </code></pre>
  
  <p>If I omit <code>age: 99</code> I get a compile error. <em>This is the behavior I expect.</em> </p>
  
  <p>However:</p>
  
  <pre><code>function putUser(user:User):void { }
  
  putUser(&lt;User&gt; {
      name: "Aaron"
  });
  </code></pre>
  
  <p>I get no compile error for missing non-optional property <code>age</code>.</p>
  
  <p>In fact in my real code-base I've found that with <strike>casting</strike> <strong>type assertion</strong> I can at times add completely wrong properties to the object literal, but I wasn't able to reproduce this behavior in a simple case like above.</p>
  
  <p>Can anyone explain what's going on here?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> Omitting the assertion works, thanks @Amid. However, where I ran into this as an issue was in this kind scenario:</p>
  
  <pre><code>interface User { name: string; }
  
  function putUser(user:User):void { }
  
  interface Musician extends User { instrument: string; }
  
  putUser(&lt;Musician&gt; {
      name: "Bob Dylan"
  });
  </code></pre>
  
  <p>The issue is that I want to validate the <code>Musician</code> object, but <code>putUser</code> only expects the base <code>User</code> object. I thought the type assertion would give me better type safety, but in this case it actually <em>reduces</em> type safety.</p>
  
  <hr>
  
  <p><strong>Edit #2</strong></p>
  
  <p>It looks like I can get what I wanted using a generic type constraint, which actually <em>looks</em> an awful lot like my type assertion:</p>
  
  <pre><code>function putUser&lt;T extends User&gt;(user:T):void { }
  
  putUser&lt;Musician&gt;({
      name: "Bob Dylan",
      instrument: "Guitar"
  });
  </code></pre>
  
  <p>In this case it seems it will make sure the object literal is exactly an implementation of <code>Musician</code>.</p>