title: Typescript - Kotlin-like copy function
body: <p>In Kotlin a <code>data class</code> can be copied using its auto-generated <code>copy</code> function.</p>
  
  <pre><code>val jack = User(name = "Jack", age = 1)
  val olderJack = jack.copy(age = 2)
  </code></pre>
  
  <p>The copy function signature, as per documentation (<a href="https://kotlinlang.org/docs/reference/data-classes.html" rel="nofollow noreferrer">Docs</a>):</p>
  
  <pre><code>fun copy(name: String = this.name, age: Int = this.age) = User(name, age)
  </code></pre>
  
  <p>Can something like this be done in TypeScript?<br>
  Is this an acceptable solution, or can it be improved somehow?</p>
  
  <pre><code>export class User {
      constructor(
              public name: string,
              public age: number) {}
  
      copy(parameters: {
          name?: string,
          age?: number
      }): User {
          const { name, age } = parameters
          return new User(name || this.name, age || this.age)
      }
  }
  </code></pre>
  
  <p>This could be solved even with a <code>Partial&lt;User&gt;</code>:</p>
  
  <pre><code>copy(partialUser: Partial&lt;User&gt;): User {
     return new User(
              partialUser.name || this.name,
              partialUser.age || this.age)
  }
  </code></pre>