Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove properties from an object that are not declared in a type interface [duplicate]

Tags:

typescript

I want to remove all properties from an object which are not declared in a specific type interface.

For example, let's assume that I have the following interface:

export interface CreateCustomerUserInput {
    fullname: string;
    email: string;
}

And I have the following object:

let obj = {fullname: 'AAA', email: '[email protected]', phone: '111', address: 'XXX'};

But I want to create a new object with only properties declared in the type interface. Here is the expected object:

let c = {fullname: 'AAA', email: '[email protected]'}

Is there any good way to solve this in TypeScript?

like image 295
Patrick Blind Avatar asked Jul 03 '18 12:07

Patrick Blind


People also ask

How to remove property in interface?

Use the Omit utility type to remove a property from an interface, e.g. type WithoutAge = Omit<Person, 'age'> . The Omit utility type constructs a new type by removing the specified keys from the existing interface. Copied! We can use the Omit utility type to remove one or more properties from an interface.

How do I delete a properties in TypeScript?

To remove a property from an object in TypeScript, mark the property as optional on the type and use the delete operator. You can only remove properties that have been marked optional from an object.

How do I omit multiple properties in TypeScript?

If you need to exclude multiple properties, you can pass a union of string literals to the Omit utility type.

What is omit in TypeScript?

TypeScript provides a number of utility types that are used to solve a particular problem that using types in Javascript creates. One very useful utility type used in TypeScript is the Omit type, which lets us customize an already existing type.


1 Answers

If I understand you correctly, you want to strip away all properties from an object that are not defined in a specific type interface.

Unfortunately, type interfaces are not available at runtime - meaning, they don't exist when running the JavaScript code. Therefore, there is no way to access reflective information about the type programmatically.

However, you might be successful by using a class. For example, let's assume you have the following class:

export class CreateCustomerUserInput {
    public fullname: string = "";
    public email: string = "";
}

You could create an instance of that class and iterate over its properties using a for.. in loop or with Object.keys(). Then you could strip away the properties of your given object by using delete for each property that is not available in the instance of your class.

For this to work, make sure all properties of the class are initialized after construction.

Another way is using TypeScript decorators with the experimental metadata flag, as explained here: http://www.typescriptlang.org/docs/handbook/decorators.html.

They preserve reflective information, but only for classes (or abstract classes)!

like image 132
ggradnig Avatar answered Oct 20 '22 09:10

ggradnig