由于某种原因我无法比较一种类型。我有以下域类:
export type QuestionType = 'TextOnly'
| 'RadioListQuestion'
| 'DropDownListQuestion'
| 'CheckBoxQuestion'
| 'NumberQuestion'
| 'DateQuestion'
| 'TextQuestion'
| 'QuestionGroup';
在我的组件中,我想比较这样的类型:
if(question.type == QuestionType.QuestionGroup.) {
// some logic here
}
但它抛出了一个编译错误:
'QuestionType' only refers to a type, but is being used as a value here.
有谁能请我指出正确的解决方案?
@Update as @betadeveloper建议我在方法定义中添加了一个类型检查,遗憾的是这不起作用:
formGroupStructure = questionGroup.questions.reduce((accumulator, question: { type: QuestionType }) => {
// If it's a nested question group (QuestionGroup inside questions[])
if(question.type === 'QuestionGroup') {
const formGroupStructure = this.createQuestionGroupFormGroupStructure(question.questionGroup);
const segmentControl: AbstractControl = formGroup.get(tab.id)['controls'][segment.id];
(<FormGroup>segmentControl).addControl(segment.questionGroup.id, this.formBuilder.group(formGroupStructure));
} else {
// treat normal questions: Add them to the accumulator
if (isQuestionAnswerable(question)) {
accumulator[question.id] = [
question.selection,
question.isMandatory ? [Validators.required] : [],
];
}
}
return accumulator;
}, {});
答案 0 :(得分:0)
只需导入并使用确切类型即可。在您的示例中,您可以执行以下操作:
if(question.type == QuestionGroup) {
// some logic here
}