Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure mapping of JSON fields to TypeScript object properties?

Input JSON:

{
  "some_field":"some value"
}

Type to be used for mapping:

export inteface SomeType {
  someField:string;
}

How can the mapping between JSON and TypeScript type be configured so that some_field -> someField?

like image 845
Andras Hatvani Avatar asked Nov 28 '16 07:11

Andras Hatvani


People also ask

How do I map JSON to interface TypeScript?

Head over to https://app.quicktype.io. Paste the string {"firstName": "Kevin", "lastName": "Le", "accountBalance": 100} to the left pane. Type User as the Name , and select JSON as the Source type . In the box on the right, select TypeScript as the Language and make sure Verify JSON.

How do you get a specific value from a JSON object in TypeScript?

To parse a JSON string in TypeScript, you can use JSON. parse().

How would you define a JSON object in TypeScript?

We can create a JSON object in TYPESCRIPT dynamically. There are two types of objects in TypeScript. A Plain Object that we achieve with json. parse() which results in the plain object, and the TypeScript Class, which returns the Class Object.


1 Answers

You could use decorators to do that . see this post for more details Type Script Decorators

like image 60
Ashley John Avatar answered Oct 20 '22 21:10

Ashley John