Frage

We have to copy objects across between a web worker and the main application. We have set up a system where, for each class that is copied across, we define an interface and then a class that implements the interface.

This works great because the JSON that is copied across can be cast to the interface and then the properties can be assigned. It's not critical to have the class implement the interface, but it's clean and probably will reduce bugs.

But we have a problem. We use the typescript-collections class. So my class will have:

export class DocHeader {

    public format:DocumentFormat;
    public fonts : collections.LinkedList<fonts.FontAttributes>;
    public styles : collections.Dictionary<string, styles.Style>;

But the JSON is an array and therefore will be:

export interface IDocHeader {

    format:DocumentFormat;
    fonts : fonts.FontAttributes[];
    styles : styles.Style[];
}

I know the answer is probably "no, not possible." But I figure it never hurts to ask. Is there some type (and not any) like IEnumerable at least that I can use in the interface that matches both uses?

War es hilfreich?

Lösung

The JSON type you're looking for is an associative array:

export interface IDocHeader {
    format: DocumentFormat;
    fonts: fonts.FontAttributes[];
    styles: { [styleId: string]: styles.Style; };
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top