如何在定义之前解决需要应用类型的问题?

时间:2017-09-08 21:05:54

标签: angular typescript typescript2.0

我走进了一个有趣的问题。这是我的类型

export interface QuestionPrimative {
    question    : string;
    id          : string;
    name        : string;
    formctrl?   : string;
    formgrp?    : string;
    lowExtreme? : string;
    hiExtreme?  : string;
    template    : string;
}

export interface Answer {
    answer      : string;
    id          : string;
    trigger?    : string;
    formctrl?   : string;
}

export interface QuestionBase extends QuestionPrimative {
    answers?: Answer[];
}

export interface MicroQuestions {
    activate?   : string;
    questions?  : QuestionBase[];  // I actually want this to be Question[]
}

export interface Question extends QuestionBase {
    micros? : MicroQuestions[];
}

我正在将此加载到我为调查问卷创建的Angular 2 Component中,以自动处理我抛出的任何问题。为了给我正在做的事情提供一个更好的例子以及为什么我用这种方式塑造一切这里是整个模型的注释版本

//The data when it iterates into Component
question    : string;           // the question to be asked
id          : string;           // id attribute for HTML
name        : string;           // name attribute for HTML
formctrl?   : string;           // if element will be a form control
formgrp?    : string;           // if element will be a form group
template    : string;           // matches *ngIf on appropriate chunk of HTML
answers?    : Answer[];         // available answers if multiple choice
lowExtreme? : string;           // low extreme if question involves rating
hiExtreme?  : string;           // hi extreme for rating
micros?     : MicroQuestions[]; // if more questions are available

//if answers are available.....
answer      : string;           // the answer
id          : string;           // id attribute for HTML
trigger?    : string;           // used to trigger another tier of questions
formctrl?   : string;           // if element needs to be a form control

//if there is another tier of questions ......
activate?   : string;           // matches with the 'trigger' in the answer to know to load
questions?  : QuestionBase[];   // the next tier of questions(which I want to have micros)

有人知道我怎么能按照需要的方式输入它来循环工作吗?

1 个答案:

答案 0 :(得分:1)

只需将QuestionBase替换为Question,就可以了。

export interface QuestionPrimative {
  question: string;
  id: string;
  name: string;
  formctrl?: string;
  formgrp?: string;
  lowExtreme?: string;
  hiExtreme?: string;
  template: string;
}

export interface Answer {
  answer: string;
  id: string;
  trigger?: string;
  formctrl?: string;
}

export interface QuestionBase extends QuestionPrimative {
  answers?: Answer[];
}

export interface MicroQuestions {
  activate?: string;
  questions?: Question[];  // Works
}

export interface Question extends QuestionBase {
  micros?: MicroQuestions[];
}