title: Axios and Typescript : How assign a typed variable from Object
body: <p>I'm currently working on a Next js App and i'm using Axios to call my external API.</p>
  <p>When i'm using Axios i can define a type to template the function like this:</p>
  <pre class="lang-js prettyprint-override"><code>export type User = {
    id: string
    username: string
    firstName: string
    lastName: string
  }
  
  const response = await axios.get&lt;User&gt;(route, config)
  
  </code></pre>
  <p>But if the API send me more properties on data, the user is not a User anymore is just an object.</p>
  <p>For example :</p>
  <p>Data received from the API</p>
  <pre><code>{
    id: &quot;foo&quot;
    username: &quot;foo&quot;
    firstName: &quot;foo&quot;
    lastName: &quot;foo&quot;
    email: &quot;foo&quot; // not in User
  }
  </code></pre>
  <p>Then i will assign my user variable with the data</p>
  <pre><code>const user: User = response.data // should be a User 
  </code></pre>
  <p>Finally, if i log my user there is spare propeties like email</p>
  <pre><code>console.log(user)
  
  // give me 
  
  {
    id: &quot;foo&quot;
    username: &quot;foo&quot;
    firstName: &quot;foo&quot;
    lastName: &quot;foo&quot;
    email: &quot;foo&quot;
  }
  
  // and not 
  
  {
    id: &quot;foo&quot;
    username: &quot;foo&quot;
    firstName: &quot;foo&quot;
    lastName: &quot;foo&quot;
  }
  
  </code></pre>
  <p>So what is the interest of the template in the get function ?</p>
  <p>and</p>
  <p>Is there a generic way to assign my typed variable with the object received and choose between fill my type with the value received and ignore spare propeties or fill my type with the value received and if there is one or more propeties in extra just return or throw an error ?</p>
  <p>Thanks</p>