0

I want to copy an object of a specific type, to another object of an other type but similar type.

Givin this example:

interface PersonInfo {
  firstName: string
  lastName: string
  age: number
  length: number
}

interface Person {
  firstName: string
  lastName: string
}

I tried

const a: PersonInfo = {
  firstName: "john",
  lastName: "Doe",
  age: 23,
  length: 180
}

const b: Person = a

I want that const b:Person = { firstName: "john", lastName: "Doe", }

I don't know if this is relevant, but I am not looking for a solution where interface PersonInfo can extend interface Person

  • You have to essentially do it explicitly; the interface definition isn't available at runtime so nothing knows which keys are "extra". See the answers to the linked question for more information. – jcalz Aug 30 '23 at 14:28

0 Answers0