用于在打字稿中初始化变量的参数化方法class

Parameterize method for initializing variables in typescript class

我的 constructor 中有以下代码来初始化变量。

public Elements: Observable<dcElementDataStoreItem>;
private elementSubjectData: BehaviorSubject<dcElementDataStoreItem>;

constructor() {
  this.elementSubjectData = new BehaviorSubject([]) as BehaviorSubject<ElementDataStoreItem>;
  this.Elements = this.elementSubjectData.asObservable();
}

现在只有 2 行,我可能有几个类似的项目。 那么我可以做类似下面的代码吗,(但这不起作用)

constructor() {
  this.initSubjectData<dcElementDataStoreItem>(this.elementSubjectData, this.Elements);
}

private initSubjectData<T>(privateList: BehaviorSubject<T>, publicList: Observable<T>) {
  privateList = new BehaviorSubject([]) as BehaviorSubject<T>;
  publicList = privateList.asObservable();
}

这样我就可以重用这个 initSubjectData 来初始化所有其他可能的方法

您不能像在其他语言中那样传递字段 "by reference",但您可以传递字段名称并通过索引访问。由于 Typescript 支持一种类型是另一种类型的键的概念,因此这将是类型安全的。

public elementSubjectData: BehaviorSubject<ElementDataStoreItem>;
public Elements: Observable<ElementDataStoreItem>;
constructor() {
    this.initSubjectData("elementSubjectData", "Elements");
}

private initSubjectData(privateListField: keyof this, publicListField:  keyof this) {
    let privateList =  new BehaviorSubject([]);
    this[privateListField] = new BehaviorSubject([]) as any;
    this[publicListField] = privateList.asObservable() as any;
}

这种方法有两个问题:

  1. 没有检查字段类型
  2. 属性必须是 public 才能使字段成为 keyof this

如果我们添加第二个函数 returns 字段的类型表示,我们可以修复数字 1:

interface Field<TName, TType> {
    name: string;
    set(value: TType) : void;
}
// In the class
constructor() {
    this.initSubjectData(this.field("elementSubjectData"), this.field("Elements"));
}
field<TKey extends keyof this>(name: TKey): Field<TKey, this[TKey]> {
    return {
        name,
        set: (value) => {
            this[name] = value
        }
    };
}
private initSubjectData<T>(privateListField: Field<keyof this, BehaviorSubject<T>>, publicListField:  Field<keyof this, Observable<T>>) {
    let privateList = new BehaviorSubject([]) as BehaviorSubject<T>;
    privateListField.set(privateList);
    publicListField.set(privateList.asObservable());
}