C # dictionary in TypeScript?

How do I implement a TypeScript interface to define these C # models:

public class PageModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public IDictionary<string, FieldModel> Fields { get; set; }
}

public class FieldModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string DataType { get; set; }
    public string DefaultValue { get; set; }
}
+4
source share
1 answer

If you are looking for interfaces only then:

interface PageModel {
    Id: number;
    Name: string;
    Fields: { [key: string]: FieldModel };
}

interface FieldModel {
    Id: number;
    Name: string;
    Type: string;
    DataType: string;
    DefaultValue: string;
}

Some differences:

  • Javascript has no concept long / integer / double, all numbers
  • In a simple map implementation based on js object notation, keys are only strings. In typescript, they are called Indexed Types.
  • As long as you get get / set using inside classes, interfaces do not have this, and you can define its property.
+9
source

Source: https://habr.com/ru/post/1650570/


All Articles