2017-09-08 4 views
0

Je suis entré dans un problème intéressant. Voici mes typesComment puis-je contourner le besoin d'appliquer un type avant qu'il ne soit défini?

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[]; 
} 

Je chargement cela en un Angular 2 Component j'ai créé pour mes questionnaires pour traiter automatiquement tous les styles de question que je jette à elle. Pour donner un meilleur exemple pour ce que je fais et pourquoi je forme de tout ce chemin est ici une version commentée du modèle entier

//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) 

Est-ce que quelqu'un sait comment je peux taper cela fonctionne dans une boucle de la façon dont il a besoin à?

+3

Eh bien, vous remplacez 'des questions? : QuestionBase []; // Je veux vraiment que ce soit Question [] 'par" questions "? : Question []; ' –

+0

oh wow, idk ce que j'ai mal fait alors parce que j'ai eu une erreur avant et quelque chose de familier résonnant du tutoriel Tour of Heros où ils ont insisté sur la création de vos types dans un ordre où ils seraient déjà définis t travail. donc je pensais que j'avais marché dans ça, mais ça marche, merci. – Optiq

Répondre

1

Il suffit de remplacer QuestionBase avec Question et il fonctionne très bien:

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[]; 
}