Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dictionary in TypeScript?

How should I implement TypeScript interface to define this 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; }
}
like image 859
monstro Avatar asked Aug 08 '16 15:08

monstro


1 Answers

If you're 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:

  1. Javascript has no notion of long/integer/double, they are all numbers
  2. In a simple map implementation which is based on the js object notation, the keys are only strings. In typescript these are called Indexable Types
  3. While you get use get/set inside classes, the interfaces don't have that and you can only define the property.
like image 162
Nitzan Tomer Avatar answered Sep 27 '22 17:09

Nitzan Tomer