将类型传递给此类的目的是什么?

问题描述 投票:0回答:1

Angular doc's cookbook for dynamic forms中,建议使用此问题的基类(它们的表单元素之一:

export class QuestionBase<T> {
  value: T;
  key: string;
  label: string;
  required: boolean;
  order: number;
  controlType: string;
  type: string;
  options: {key: string, value: string}[];

  constructor(options: {
      value?: T,
      key?: string,
      label?: string,
      required?: boolean,
      order?: number,
      controlType?: string,
      type?: string
    } = {}) {
    this.value = options.value;
    this.key = options.key || '';
    this.label = options.label || '';
    this.required = !!options.required;
    this.order = options.order === undefined ? 1 : options.order;
    this.controlType = options.controlType || '';
    this.type = options.type || '';
  }
}

并且在使用它时,会传入一个类型数据

export class TextboxQuestion extends QuestionBase<string> {/* input-type specific code */}
                                                  ^^^^^^
export class DropdownQuestion extends QuestionBase<string> {/* input-type specific code */}
                                                   ^^^^^^

我不明白您为什么要将此类型传递给QuestionBase类。广义地说,将类型传递给这样的类为什么有用?在这个特定的示例中,为什么要传递可选属性的类型(不确定它是否是可选属性),此外,对于HTML输入,据我所知,它们都返回字符串,所以目的是什么?] >

在Angular doc的动态表单食谱中,建议使用此问题的基类(它们的表单元素之一):导出类QuestionBase {值:T;键:字符串;标签:...

javascript angular typescript
1个回答
0
投票

这是在TypeScript中编写泛型的方式

© www.soinside.com 2019 - 2024. All rights reserved.