1
  interface A { a: string; x?: string }
  interface AB extends A { b: string }

  let a: A = { a : 'initial' }
  let ab: AB = { a: 'alex', b: 'bolbets', x: 'hello world!' };

  a = Object.keys(ab).forEach((key) => if (key in a) a[key] = ab[key])
  // Result: a = { a: 'alex' }
  // Desired output:  a = { a: 'alex', x: 'hello world!' }
  a = ab as A // Or `as A & AB`
  // Result: a = { a: 'alex' b: 'bolbets', x: 'hello world!' }
  // Desired output:  a = { a: 'alex', x: 'hello world!' }
Alex Pappas
  • 2,377
  • 3
  • 24
  • 48

0 Answers0