title: Cast object literal to subinterface
body: <p>Take a look at following code: <a href="https://www.typescriptlang.org/play/#src=interface%20A%20%7B%0D%0A%20%20get()%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Smth1%20%7B%0D%0A%20%20public%20x%3A%20A%20%3D%20%7B%20value%3A%202%2C%20get()%20%7B%20return%20this.value%20%7D%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Smth2%20%7B%0D%0A%20%20public%20x%3A%20A%20%3D%20%7B%20value%3A%202%2C%20get()%20%7B%20return%20this.value%20%7D%20%7D%20as%20A%0D%0A%7D%0D%0A%0D%0Aclass%20Smth3%20%7B%0D%0A%20%20public%20x%3A%20A%0D%0A%0D%0A%20%20constructor()%20%7B%0D%0A%20%20%20%20const%20x%20%3D%20%7B%20value%3A%202%2C%20get()%20%7B%20return%20this.value%20%7D%20%7D%0D%0A%20%20%20%20this.x%20%3D%20x%0D%0A%20%20%7D%0D%0A%7D" rel="nofollow noreferrer">typescript playground</a> (turn on <code>noImplicitThis</code> flag)</p>
  
  <pre><code>interface A {
    get(): number;
  }
  
  class Smth1 {
    public x: A = { value: 2, get() { return this.value } }
  }
  
  class Smth2 {
    public x: A = { value: 2, get() { return this.value } } as A
  }
  
  class Smth3 {
    public x: A
  
    constructor() {
      const x = { value: 2, get() { return this.value } }
      this.x = x
    }
  }
  </code></pre>
  
  <p>Both <code>Smth1</code> and <code>Smth2</code> have compilation errors:</p>
  
  <pre><code>public x: A = { value: 2, get() { return this.value } }
  </code></pre>
  
  <blockquote>
    <p>Object literal may only specify known properties, and 'value' does not exist in type 'A'.</p>
    
    <p>Property 'value' does not exist on type 'A'.</p>
  </blockquote>
  
  <pre><code>public x: A = { value: 2, get() { return this.value } } as A
  </code></pre>
  
  <blockquote>
    <p>Property 'value' does not exist on type 'A'.</p>
  </blockquote>
  
  <p>Only <code>Smth3</code> has no compilation errors.</p>
  
  <p>That means that I must add explicit constructor for my class and split assignment into two statements: temporary variable and assign it to a class field.</p>
  
  <p>As for me, it seems to much code for such thing.</p>
  
  <p><strong>How can I assign object literal with extra fields as an interface type without using <code>as any</code>?</strong></p>