为什么打字稿没有给出编译错误

时间:2020-03-28 18:16:20

标签: typescript

在以下代码中,additional-info的类型应为QuestionsCreatedByUserForATagContext|StatusBarContext|PracticeQuestionContext| PracticeQuestionListContext|QuestionDetailContext;

export class DialogServiceContext {
  public ['sender-id']: ComponentAndServiceID;
  public ['additional-info']: QuestionsCreatedByUserForATagContext|StatusBarContext|PracticeQuestionContext|
    PracticeQuestionListContext|QuestionDetailContext;
  public ['dialog-type']: DialogType;
  constructor( senderID: ComponentAndServiceID,
               additionalInfo: QuestionsCreatedByUserForATagContext|StatusBarContext|PracticeQuestionContext|
                 PracticeQuestionListContext|QuestionDetailContext,
               dialogType: DialogType= DialogType.alert) {
    this['sender-id'] = senderID;
    this['additional-info'] = additionalInfo;
    this['dialog-type'] = dialogType;
  }
}

但是我仍然可以按如下方式使用它(向它传递SignupContext类型的上下文。

showDialog(context: SignupContext) {
    this.dialogService.emitDialogMessage(new DialogServiceContext(ComponentAndServiceID.SignupComponent, context));

  }

为什么typescript没有给出编译错误?

0 个答案:

没有答案