比在打字稿中使用 setter 更好的方法:声明了私有变量但未读取值问题
Better Method than using setter in typescript : private variables are declared but value not read issue
我有这样一个场景:
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 中收到一条警告,例如 "private variables are declared but its value is not read"。
为了防止这个警告,我必须使用 getter 方法,但这里的目的是保存对象属性而不是读取。所以 getter 方法似乎没有用。
由于我是 typescript 的新手,没有将这些变量设置为 public 或在 tslint 配置中禁用,有人可以提出更好的方法吗?
目的是设置员工信息,为此我创建了Employee模型。
任何帮助将不胜感激。
提前致谢
由于除了分配给它的属性外,您没有对这一端的数据执行任何操作,所以听起来您应该创建一个普通对象。由于在您的原始代码中,所有设置属性的方法都是 public,并且不做任何其他事情,所以它们没有完成任何有用的事情。如果外部源可以调用 setter 方法,它也可以直接分配 属性 。 class
增加了不必要的和令人困惑的开销,部分原因是 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
IMO,class 通常仅当您需要将数据与实例相关联时才有用 and 以某种方式检索和使用数据的方法。
我有这样一个场景:
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 中收到一条警告,例如 "private variables are declared but its value is not read"。 为了防止这个警告,我必须使用 getter 方法,但这里的目的是保存对象属性而不是读取。所以 getter 方法似乎没有用。 由于我是 typescript 的新手,没有将这些变量设置为 public 或在 tslint 配置中禁用,有人可以提出更好的方法吗?
目的是设置员工信息,为此我创建了Employee模型。
任何帮助将不胜感激。
提前致谢
由于除了分配给它的属性外,您没有对这一端的数据执行任何操作,所以听起来您应该创建一个普通对象。由于在您的原始代码中,所有设置属性的方法都是 public,并且不做任何其他事情,所以它们没有完成任何有用的事情。如果外部源可以调用 setter 方法,它也可以直接分配 属性 。 class
增加了不必要的和令人困惑的开销,部分原因是 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
IMO,class 通常仅当您需要将数据与实例相关联时才有用 and 以某种方式检索和使用数据的方法。