2

I have an object with additional properties but only want to return certain properties defined by an interface to the backend.

Is there a clean solution to this? I have checked online and a lot seem to refer to the Omit, Pick etc. utility methods. However, I have tried that but it returns all the properties of the object and does not omit/pick the required properties.

interface Student{
name: string;
age: number;
address: string;
}

an example object:

{name: "John Doe", age: 30, address: "123 ...", phoneNumber: "xxxxx", nextOfKin: "Marie Doe", ....}

I have tried:

type StudentInfoRequest = Omit<Student, "phoneNumber" | "nextOfKin">
type StudentInfoRequest = Pick<Student, "name" | "age" | "address">

when I assign it e.g.

let studentRequestList: StudentInfoRequest[] = [];

looping over the student objects to push into the studentRequestList and when I console log it includes the entire properties of the object rather than the interface only properties.

Gintoki
  • 41
  • 3
  • If you have tried something, then why not present the code of what you tried? Ideally a [mre]. – Joachim Sauer Aug 26 '22 at 10:12
  • 1
    @JoachimSauer I have added part of my code into the description – Gintoki Aug 26 '22 at 10:22
  • 4
    Types don't affect the runtime behaviour of your program. You will have to use JavaScript to remove these properties. – Tobias S. Aug 26 '22 at 10:22
  • @TobiasS. That is interesting to know especially since types is mentioned quite a lot as a solution unless I have misunderstood it. – Gintoki Aug 26 '22 at 10:41
  • Does this answer your question? [How to get a subset of a javascript object's properties](https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties) – GreyBeardedGeek Aug 26 '22 at 10:46

0 Answers0