Home > Web Front-end > JS Tutorial > How can we ensure that an object only contains properties defined in its interface when passing it through angular.toJson?

How can we ensure that an object only contains properties defined in its interface when passing it through angular.toJson?

Mary-Kate Olsen
Release: 2024-10-31 16:28:02
Original
216 people have browsed it

How can we ensure that an object only contains properties defined in its interface when passing it through angular.toJson?

Ensuring Object Compliance with Interface: Removing Extraneous Properties

TypeScript interfaces define contract specifications for classes or objects. However, during implementation, additional properties may be added beyond those defined by the interface, leading to inconsistencies. Addressing this issue becomes crucial, especially when passing reduced objects through angular.toJson for RESTful communication.

Consider an interface MyInterface with a single property test:

<code class="typescript">interface MyInterface {
  test: string;
}</code>
Copy after login

And its implementation with an additional property newTest:

<code class="typescript">class MyTest implements MyInterface {
  test: string;
  newTest: string;
}</code>
Copy after login

Question:

How can we ensure that the reduced object, when assigned from an object like MyTest, only contains the properties declared in MyInterface, excluding newTest?

Answer:

Unfortunately, it's not feasible to directly strip extraneous properties from an object at runtime based solely on an interface definition. Interfaces in TypeScript serve as a design-time construct, and their properties aren't readily available at execution.

One potential solution suggested is to define the "interface" as a class, thereby providing a runtime implementation. This allows us to leverage Lodash to select only the desired properties from the input object:

<code class="typescript">class MyInterface {
  test: string = undefined;
}</code>
Copy after login
<code class="typescript">import _ from 'lodash';

const before = { test: "hello", newTest: "world"};
let reduced = new MyInterface();
_.assign(reduced , _.pick(before, _.keys(reduced)));
console.log('reduced', reduced)//contains only 'test' property</code>
Copy after login

By employing this method, we can effectively extract the specified properties, creating an object that adheres to the interface contract and is ready for serialization via angular.toJson.

The above is the detailed content of How can we ensure that an object only contains properties defined in its interface when passing it through angular.toJson?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template