Javascript 比在typescript中使用setter更好的方法:声明私有变量,但不读取值

Javascript 比在typescript中使用setter更好的方法:声明私有变量,但不读取值,javascript,node.js,typescript,class,getter-setter,Javascript,Node.js,Typescript,Class,Getter Setter,我有这样一个场景: class Employee { private fullName: string; private firstName: string; private lastName: string; private age: number; private isValid: boolean; constructor() { this.fullName = null; this.firstName = null; this.lastNa

我有这样一个场景:

class Employee {
  private fullName: string;
  private firstName: string;
  private lastName: string;
  private age: number;
  private isValid: boolean;

  constructor() {
    this.fullName = null;
    this.firstName = null;
    this.lastName = null;
    this.age = null;
    this.isValid = false;
  }

  setfullName(fullName: string) {
    this.fullName = fullName;
  }

  setfirstName(firstName: string) {
    this.firstName = firstName;
  }

  setlastName(lastName: string) {
    this.lastName = lastName;
  }

  setAge(age: number) {
    this.age = age;
  }

  setValid(isValid: boolean) {
    this.isValid = isValid;
  }
}

// test.ts file
let employee = new Employee();

// set in different positions in test.ts file based on getting the input paramters
employee.setfullName("james cooper");
employee.setfirstName("mary");
employee.setlastName("fransis"); // getting lastName from another api call
employee.setAge(50); // getting 50 from another api call
employee.setValid(true);
在这里,我在vscode中得到一个警告,比如“声明了私有变量,但未读取其值”。 为了防止这个警告,我必须使用getter方法,但这里的目的是保存对象属性而不是读取。所以getter方法似乎是无用的。 由于我是typescript新手,在没有将这些变量设置为public或在tslint配置中禁用的情况下,有人能为同样的问题提出更好的方法吗

目的是设置员工信息,我为此创建了员工模型

任何帮助都将不胜感激


提前感谢

因为您除了为数据的属性赋值之外,没有对数据进行任何处理,所以听起来您应该创建一个普通对象。因为在您的原始代码中,所有设置属性的方法都是公共的,并且不做任何其他事情,所以它们不能完成任何有用的事情。如果外部源可以调用setter方法,那么它也可以直接分配一个属性。
增加了不必要且令人困惑的开销,这也是Typescript抱怨的部分原因。相反,请执行以下操作:

type Employee = Partial<{
    fullName: string;
    firstName: string;
    lastName: string;
    age: number;
    isValid: boolean;
}>;
const employee: Employee = {};
employee.age = 15;
employee.isValid = false;
// send employee to front-end
type Employee=Partial;
const employee:employee={};
雇员年龄=15岁;
employee.isValid=false;
//将员工发送到前端

依我看,只有当您需要与实例关联的数据以及以某种方式检索和使用数据的方法时,类通常才有用。

您没有任何getter,因此,这些值实际上是不被读取的,也不能被读取,因为它们是私有的。如果数据永远不会被读取,那么保存数据的目的是什么?在不读取数据的情况下保存数据有什么意义?@vicnoob:我们将这些employee对象直接传递到前端。那么这应该是公共的吗?私有方法,变量只能在该类中使用