类型'()=> x'中缺少属性'x',但类型'x'中必需

时间:2019-05-30 04:04:39

标签: arrays angular typescript

错误:Property 'Control' is missing in type '() => Controls' but required in type 'Controls'.

export class Controls {
   Control: Control[];
}

page.Sections.push({
    ....
    Controls: () => {
      const c = new Controls();
      c.Control = new Array<Ctrl>();
      section.VisualComponents.forEach(vc => {
          c.Control.push({
            ....
            ....

        });
      });
      return c;
    }
  });

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您必须具有Controls[]数组,但要分配一个函数Controls: () => {

export class Controls {
   Control: Control[]; /// HERE
}

page.Sections.push({
    ....
    Controls: () => { // HERE 
      const c = new Controls();
      c.Control = new Array<Ctrl>();
      section.VisualComponents.forEach(vc => {
          c.Control.push({
            ....
            ....

        });
      });
      return c;
    }
  });

修复

调用函数以获取其返回值,例如:

const createControls = () => {
      const c = new Controls();
      c.Control = new Array<Ctrl>();
      section.VisualComponents.forEach(vc => {
          c.Control.push({
            ....
            ....

        });
      });
      return c;
    };
export class Controls {
   Control: Control[];
}

page.Sections.push({
    ....
    Controls: createControls()
  });