Just dipped my toes into the world of TypeScript and I'm curious about the best approach to handle this scenario.
I've got some JSON data coming from an API, like so:
{"name" : "A Person", "age": "25", createdAt: "timestamp"}
And I've defined an interface IPersonData to represent this incoming JSON.
export interface IPersonData {
name: string;
createdAt: string;
age: string;
}
However, I also have an actual Person Object within the system:
export interface IPerson extends IPersonData {
createdAt: DateTime; //this is the luxon DateTime
age: number;
}
While Webstorm seems fine with this (it even provides an icon indicating it's overridden), the compiler isn't happy, throwing the error message:
Type 'DateTime' is not assignable to type 'string'.
I even attempted the following workaround:
export interface IPerson extends Omit<IPersonData, "createdAt">{
createdAt: DateTime; //this is the luxon DateTime
age: number;
}
Is it possible to override in TypeScript? If not, would it be worth representing the incoming JSON to ensure consistency with the JSON leaving the API?