javascript - No se puede establecer la propiedad XXX de indefinido en TypeScript
Quiero inicializar una interfaz en TypeScript.
export interface TestGroup {
"id": number;
"type": TestType;
"interval"?: number;
}
import {TestGroup} from "../../gen/api";
public functionConfig: TestGroup;
.
.
.
this.functionConfig.id = testGroup.functionTestConfig;
Obtengo este error:
TypeError: No se puede establecer la propiedad 'id' de indefinido
¿Podrías ayudarme, por favor?
Respuesta #1
Tu functionConfig tiene un tipo, pero en realidad es undefined. Por lo tanto, necesitas inicializarlo:
public functionConfig: TestGroup = {id: 0, type: 'YourTestTypeHere'};
o tus valores predeterminados para ello
Comentario
var activateOnWeekDay: Array<boolean> = [];
Respuesta #2
Puedes arreglarlo inicializando un object vacío {}.
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;
