Creating an instance of a class in TypeScript

I have the following class structure

module ChartingServices {

export class ChartOptions {
    options: myOptions;
    title: string;
}

export interface myOptions {
    colors: string[];
    legend: myLegend;
    type: string;
}

export interface myLegend{
    enabled: boolean;
    labelFormatter: string;
}
}

and I instantiate it in the usual way: -

var chartOptions = new ChartingServices.ChartOptions();

I can set the chartOptions.title property without any problems.

However, I cannot get to the chartOptions.myOptions.type property and get the error "Unable to read property" type "undefined.

Given that I have many classes, I need to create an instance of each of them to set / read properties. What can I do to make the code work?

+4
source share
2 answers

, .myOptions chartOptions - options. : MyOptions, MyLegend, .

, , chartOptions:

var chartOptions = new ChartOptions();

... options , undefined. :

chartOptions.options = {type: "foo", ...other non-optional properties here}

chartOptions :

options: MyOptions = {type: "foo", ...}
+7

TypeScript JavaScript ( ) : undefined, .

, . , . , ?, ,

export class ChartOptions {
    options: myOptions = {};
...
0

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


All Articles