在打字稿中声明嵌套数组

时间:2019-04-20 10:11:57

标签: arrays typescript

我有以下类,用户可以通过表单实例化

import { User } from "./User";
import { LearningObjective } from "./LearningObjective";
import { Structure } from "./Structure";

export class Technique {
  id                    : string;
  name                  : string;
  description           : string;
  rules                 : string[];
  delivery_mode         : string[];
  interaction           : string[];
  interrelationship     : string[];
  motivation            : string[];
  participation         : string[];
  performance           : string[];
  resolution_scope      : string[];
  feedback_use          : string[];
  target_audience       : number[];
  learning_objectives   : LearningObjective[];
  affective_objectives  : string[];
  social_objectives     : string[];
  structure             : Structure;
  psychologist          : User;
}

我的问题出在结构字段上,因为用户会将字段定义为模块数组,而模块依次包含一个相位数组,每个阶段都可以包含一个任务数组

export class Task {
  type        : string;
  description : string;
  role        : string;
  resources   : string[];
}

export class Phase {
  name  : string;
  tasks : Task[];
}

export class Module {
  name   : string;
  phases : Phase[];
}

export class Structure {
  modules : Module[];
}

我已经声明了以下表格

  form  : Partial<Technique> = {
    name                  : "",
    description           : "",
    rules                 : [],
    delivery_mode         : [],
    interaction           : [],
    interrelationship     : [],
    motivation            : [],
    participation         : [],
    performance           : [],
    resolution_scope      : [],
    feedback_use          : [],
    target_audience       : [],
    learning_objectives   : [],
    affective_objectives  : [],
    social_objectives     : [],
    structure             : {
      modules: [{
        name: "",
        phases: [{
          name: "",
          tasks: []
        }]
      }]
    }
  };

但是,当我尝试定义自己的结构时

let Module = {name: "teste", phases: [{name: "phase", tasks: [{type: "tipo", description: "", role: "", resources: []}]}]};
this.form.structure.modules.push(Module);

我收到以下错误:

error TS2339: Property 'phases' does not exist on type 'Module[]'.

如何声明我的form.structure?

0 个答案:

没有答案