Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Create interface based on object

I want to create file factory (e. g. JSON with translations in my case).

{
    "field": "",
    "group": {
        "field_1": "",
        "field_2": ""
    },
    ...
}

I wish to create a template JSON with all fields that are present in my translations, and then instantiate it with some default values for each locale to allow my application not to miss any translation fields. Well, that is pretty simple, at output I have couple of files (based on count of locales), named <locale>.json, e.g. en.json with some content like this:

{
    "field": "en:field",
    "group": {
        "field_1": "en:group.field_1",
        "field_2": "en:group.field_2",
    },
    ...
}

Now I want to create a type or interface based on my JSON template to allow my translation fields to be displayed in quick suggests of my IDE (e.g. VS Code).

Is there any possibilities to do this in convenient way? I know that I can dynamically create a .ts file with exported interface, but this is not so good because all ts syntax will be provided through string, so there could be some mistakes during creation. May be there is any legal ways?

To be clear, I want to get an interface like this

interface IMyCoolInterface {
    field: string,
    group: {
        field_1: string,
        field_2: string,
    },
    ...
}
like image 767
Limbo Avatar asked Sep 01 '26 10:09

Limbo


2 Answers

You could use the --resolveJsonModule compiler option introduced in TypeScript 2.9. Then you could import the template file as a module:

import * as translationTemplate from 'template.json';

and define a type for it using a typeof type query:

type Translation = typeof translationTemplate;

If all goes well, if you declare a variable to be of type Translation you should get IDE hints:

declare const t: Translation; // or whatever
t.field; // hinted at you
t.group.field_1; // likewise
like image 181
jcalz Avatar answered Sep 03 '26 00:09

jcalz


I think that a good solution will be:

  • First declare an interface (or interfaces) depending on your JSON data structure
  • Second you can implement the interface and even add some methods if you want.

An example of a simple implementation would be:

interface IGroup{
 field_1:string;
 field_2:string;
}

interface IMyCoolInterface{
 field:string;
 group:IGroup;
}

if you want a JSON Array of groups:

interface IMyCoolInterface{
 field:string;
 groups:Array<IGroup>;
}

Now you must implement your interface like the following: First implement the IGroup interface:

class Group implements IGroup{
 field_1:string;
 field_2:string;
 construdtor(field_1:string,field_2:string)
 {
  this.field_1=field_1;
  this.field_2=field_2;
 }
}

Now implement the IMyCoolInterface (supposing that you want a JSON Array of groups):

class MyCoolClass implements IMyCoolInterface
{
 field:string;
 groups:Array<IGroup>;
 constructor(field:string,groups?:Array<IGroup>)
 {
  this.field=field;
  this.groups=groups || [];
 }
 //add some methods
 addGroup(group:IGroup)
 {
  this.groups.push(group)
 }
}

This is a simple way to deal with JSON using interfaces.

like image 21
NEO Avatar answered Sep 02 '26 22:09

NEO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!