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!' }
1
  • 2
    You can't. Type information is erased in compilation, it's not available at runtime. Commented Jun 10, 2022 at 6:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.