Assigning an object to type interface but removing properties not included in the interface?

Viewed 40

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.

0 Answers
Related