DISCLAIMER: this is purely an example of a possible approach, this doesn't mean that it needs to be valid or whatever, this is just the approach I've been using lately and allows elasticity, which is what I was looking for.
Now, onto the point.
Since interfaces don't exists in runtime, if you want to parse and effectively add any sort of "test" to acquire the desired data and eventually transform the data acquired, you should be using classes.
Here is a little example, simulating this response:
const json_response = {
status: "done",
refere: {
"id": 13,
"name": "John"
}
};
In our case, the logic I'm going to apply is having a Response Handler that takes care of the whole response, and a Referee
class that handles the refere reference aswell.
Starting from the Referee:
interface IReferee {
id: number,
name: string
}
class Referee implements IReferee {
public id: number;
public name: string;
private rawData: any;
constructor(data?: any) {
if (data !== null) {
// Do some check here to check whether data is effectively coherent.
this.id = +data.id;
this.name = data.name;
}
}
// This is the advantage of using a class.
// Adding optional methods that may be useful here and there.
get ID(): number {
return this.id;
}
get LowerCaseName(): string {
return this.name.toLowerCase();
}
get Name(): string {
return this.name;
}
get UpperCaseName(): string {
return this.name.toUpperCase();
}
}
The interface is NOT mandatory, it's just useful to be sure that you implement everything correctly in the class itself. The advantage here is that you can implement your own method if you want to (as seen above).
The response handler:
interface IMyJsonResponseHandler {
status: string,
refere: Referee
}
class MyJsonResponseHandler implements IMyJsonResponseHandler {
private rawData: any;
public status: string;
public refere: Referee;
constructor(data?: any) {
if (data !== null) {
this.status = data.status;
this.refere = new Referee(data.refere);
}
}
get Refere(): Referee {
return this.refere;
}
}
And the same criteria is applied: basically, the param provided here is the json response. Every single handling is done in the constructor, if you want to add any strict check, do it there and eventually throw errors or whatever you need there. In this way, you can access pretty much everything of your json response and you can enjoy the intellisense thanks to interfaces and classes together. Of course you can simply accept that the JSON response is typeless, but if you need to do some strict checks and need such data for other calculations or simply want to have an easier understanding of what the properties are, then you must use classes and do checks there, because interfaces in typescript are nothing but a comfort for the developer in this case.
Example usage:
const json_response = {
status: "done",
refere: {
"id": 13,
"name": "John"
}
};
let res = new MyJsonResponseHandler(json_response);
console.log(res.Refere.LowerCaseName);
Playground with this example
which logs: john
.
Advantage of this criteria: intellisense.
Alternatively, you can just easily implement an interface, but if you need complex data elaboration (or, more easily, if you need an inner property to be instantialized as a class instance) you must use other criteria.
Other sources you should check:
How to parse JSON string in Typescript
How do I cast a JSON object to a typescript class