Typescript对象的构造函数中Object

Typescript对象的构造函数中Object

本文介绍了Typescript对象的构造函数中Object.assign()的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一路上,我在 Todo 类中添加了一个构造函数:

Somewhere along the way, I added a constructor to my Todo class:

export class Todo {
  id: number;
  title: string;
  complete: boolean = false;
  editMode: boolean = false;

  constructor(values: Object = {}) {
    Object.assign(this, values);
  }
}

我不了解代码的目的构造函数。

I don't understand the purpose of the code in the constructor.

我的应用程序可以同时使用和不使用它,但是我很犹豫删除代码

My application seems to work both with and without it, but I am hesitant to remove the code

什么

推荐答案

此方法可轻松添加以下对象的值吗?将类的参数传递给它们各自的类字段,在类字段中,类将实现该接口,或者至少具有部分植入该接口的对象。

This a method to easily add the values of the parameters of a class to their respective class fields where a class implements that interface or at least has a partial implantation of that interface.

interface IPerson {
  firtName: string;
  lastName: string;
}

class Person implements IPerson {
  public firtName!: string;
  public lastName!: string;

  constructor(params: IPerson) {
    Object.assign(this, params);
  }
}

您的应用程序可以正常工作,因为您似乎已在这样的方式使得值的回调值也足够

Your application works because you seem to have implemented this in such a way that the callback value of values to also be enough.

此Hack的主要问题是 Object.assign类型不安全。因此,以这种方式使用它与TypeScript背道而驰。

The main issue with this Hack is that Object.assign is not type safe. So using it in this way in a way goes against the point of TypeScript.

如果要以类型安全的方式执行此操作,最好使用可正确检查类型的自定义实现。像这样:

If you want to do this in a type safe fashion you are better off using a custom implementation where the type is properly checked. Something like this:

type PDM = PropertyDescriptorMap;

export class ClassSAssign<T> {
  constructor(private objectToSpread: T, private klass: T) {}

  private propertyDescriptorOptions = {
    enumerable: true,
    writable: true
  };

  public apply(): void {
    const map = this.getPropertiesDescriptorMap();
    Object.defineProperties(this.klass, map);
  }

  private getPropertiesDescriptorMap(): PDM {
    return Object.entries(this.objectToSpread).reduce(
      (obj: PDM, entry) => this.getPropertyDescriptorMap(obj, entry),
      {}
    );
  }

  private getPropertyDescriptorMap(obj: PDM, [key, value]: [string, any]): PDM {
    return {
      ...obj,
      [key]: {
        value,
        ...this.propertyDescriptorOptions
      }
    };
  }
}

,您可以使用以下实用程序:

and you can use this utility like this:

class Person implements IPerson {
  public firtName!: string;
  public lastName!: string;

  constructor(params: IPerson) {
    new ClassSAssign(params, this).apply();
  }
}

如果您不想/不想使用上面的方法,建议您至少添加一些类型严谨性,以保护您的类免受可以传入值的破坏

If you don't/can't want to use the above, I suggest you at least add some type rigour to protect your class from what values can be passed into it

interface IToDo {
  id?: number;
  title?: string;
}

export class Todo implements IToDo {
  public id?: number;
  public title?: string;
  public complete: boolean = false;
  public editMode: boolean = false;

  constructor(values?: IToDo) {
    Object.assign(this, values);
  }
}

这篇关于Typescript对象的构造函数中Object.assign()的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!