我想在TypeScript中初始化一个接口。无法在TypeScript中设置未定义的属性XXX
export interface TestGroup {
"id": number;
"type": TestType;
"interval"?: number;
}
import {TestGroup} from "../../gen/api";
public functionConfig: TestGroup;
.
.
.
this.functionConfig.id = testGroup.functionTestConfig;
我得到这个错误:
TypeError: Cannot set property 'id' of undefined
你能帮帮我吗?
需要functionConfig初始化为某个值,现在它是undefiend,你最终会做undefined.id这是无效的JavaScript。 – toskv
这是什么? 'this.functionConfig'集合在哪里?我们应该猜测? – Amy
那么你声明'functionConfig',但你不给它任何值,因此它是未定义的。如果你做了'this.functionConfig = {test:“value”}'或者甚至只是'this.functionConfig = {}'就会被定义,所以之后你可以做'this.functionConfig.id =“whatever” ' –