Reputation: 1478
I want to initialize an interface in TypeScript.
export interface TestGroup {
"id": number;
"type": TestType;
"interval"?: number;
}
import {TestGroup} from "../../gen/api";
public functionConfig: TestGroup;
.
.
.
this.functionConfig.id = testGroup.functionTestConfig;
I get this error:
TypeError: Cannot set property 'id' of undefined
Could you please help me?
Upvotes: 4
Views: 7980
Reputation: 853
You can fix it by initializing an empty object
{}.
export interface TestGroup {
id: number;
type: TestType;
interval?: number;
}
import {TestGroup} from "../../gen/api";
public functionConfig: TestGroup = {}; <=====Assign empty object
.
.
.
this.functionConfig.id = testGroup.functionTestConfig;
Upvotes: 1
Reputation: 68645
Your functionConfig
has a type but it is actually undefined
. So you need to initialize it:
public functionConfig: TestGroup = {id: 0, type: 'YourTestTypeHere'};
or your default values for it
Comment
var activateOnWeekDay: Array<boolean> = [];
Upvotes: 3