带有getter的typescript可选属性

带有getter的typescript可选属性,typescript,optional,getter,object-literal,Typescript,Optional,Getter,Object Literal,这是一个简化的示例: class PersonParms{ name:string; lastName:string; age?:number; get fullName(){return this.name + " "+this.lastName;} } class Person{ constructor(prms:PersonParms){ } } new Person({name:'John',lastName:'Doe'}) // t

这是一个简化的示例:

class PersonParms{
    name:string;
    lastName:string;
    age?:number;
    get fullName(){return this.name + " "+this.lastName;}
}

class Person{
    constructor(prms:PersonParms){
    }
}

new Person({name:'John',lastName:'Doe'})  // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.
其思想是将literal对象作为PersonParms的intizalizer传递,但是拥有该getter,您既不能将getter声明为可选的,也不能将属性添加到对象literal。有没有其他方法可以实现这一目标

有没有其他方法可以实现这一目标

我会这样做:

class Person {
  constructor(public config: { name: string, lastName: string }) {}
  age?: number;
  get fullName() { return this.config.name + " " + this.config.lastName; }
}

new Person({ name: 'John', lastName: 'Doe' }) 

非常有趣。我认为,您应该使用TypeScript,因为方法可以是可选的(见下文),但属性获取程序不是。真奇怪。。作为解决方法,我可以建议两种变体。不错的一个:

class PersonParms {
    name:string;
    lastName:string;
    age?: number;

    getFullName?() {return this.name + " "+this.lastName;}
}
第二个是hacky,因为在这里,当传递给构造函数时,所有属性都是可选的

class PersonParms {
    name:string;
    lastName:string;
    age?: number;

    get fullName(){return this.name + " "+this.lastName;}
}

class Person{
    constructor(prms: Partial<PersonParms>){
    }
}
class个人密码{
名称:字符串;
lastName:string;
年龄:数字;
get-fullName(){返回this.name+“”+this.lastName;}
}
班主任{
建造商(prms:部分){
}
}

我找到了适合我的解决方案:

class Person {
  name?:string;
  lastName?:string;
  age?: number;
  fullName?:string;

  constructor(public config: { name: string, lastName: string }) {
    Object.defineProperty(this,'fullName',{
           get(){return this.name + " " + this.lastName;}
          });

}

如果创建PersonParms的新实例,则错误将消失

class PersonParms{
    name:string;
    lastName:string;
    age?:number;
    get fullName(){return this.name + " "+this.lastName;}
}

class Person{
    constructor(prms:PersonParms){
    }
}

const personParams = new PersonParms();
personParams.name = 'John';
personParams.lastName = 'John';
new Person(personParams)  // No error because this is an instance of PersonParams
我不确定您在何处/如何使用PersonParms.fullname,但在您的情况下,我将使用以下内容:

interface PersonParms{
    name:string;
    lastName:string;
    age?:number;    
}

class Person implements PersonParms{
    name: string;
    lastName: string;
    age?:number
    constructor(prms: PersonParms) {
        this.name = prms.name;
        this.lastName = prms.lastName;
        this.age = prms.age;
    }

    get fullName(){return this.name + " "+this.lastName;}
}

const person = new Person({ name: 'John', lastName: 'Doe' });

console.log(person.fullName); // John Doe

截至2020年4月,没有的实施方法

对于这一点,有一个不确定的PR:

此处介绍了通过接口提出的解决方案:

就我个人而言,这个解决方案不符合我的需要,我更愿意宣布该物业为私有


希望我们将来能有更好的运气

如果强制转换对象,这将防止编译时错误

export class IndividualModel {
    constructor(individual: IndividualModel = null) {
        if (individual) {
            this.individualKey = individual.individualKey;
            this.firstName = individual.firstName;
            this.lastName = individual.lastName;
        }
    }

    individualKey: string;
    firstName?: string;
    lastName?: string;
    get fullName(): string {
        return `${this.lastName}, ${this.firstName}`;
    }
}
const individual=new IndividualModel({individualKey:'some key'});

是的,但是我将使用PrimPARMS作为其他几个类中的基类,因此可选的吸收器应该在PrimPARNS中不必在所有子类中重复它。考虑定义接口<代码>接口IfPrimPs{{No:String;LasNetry:String;AsHoo::No.RealOnLoNoNo::String;}< /Cord>。将object literal强制转换到类似乎没有什么用处-无论如何getter不会神奇地出现在那里,您需要创建一个
PersonParms
类的实例。在初始化过程中必须为(有意)只读的属性指定一个值,这是非常容易产生误解的,这就是本文所描述的情况。嗨,Akshay,除了显示代码之外,你还应该解释你所做的更改以及它解决问题的原因。在这里,我只做了一个更改。全名?:字符串=this.name+“”+this.lastName;它是在构造函数中设置的。不是getter setter的替代品
getFullName?()
不是有效的typescript语法
interface PersonParms{
    name:string;
    lastName:string;
    age?:number;    
}

class Person implements PersonParms{
    name: string;
    lastName: string;
    age?:number
    constructor(prms: PersonParms) {
        this.name = prms.name;
        this.lastName = prms.lastName;
        this.age = prms.age;
    }

    get fullName(){return this.name + " "+this.lastName;}
}

const person = new Person({ name: 'John', lastName: 'Doe' });

console.log(person.fullName); // John Doe
export class IndividualModel {
    constructor(individual: IndividualModel = null) {
        if (individual) {
            this.individualKey = individual.individualKey;
            this.firstName = individual.firstName;
            this.lastName = individual.lastName;
        }
    }

    individualKey: string;
    firstName?: string;
    lastName?: string;
    get fullName(): string {
        return `${this.lastName}, ${this.firstName}`;
    }
}
const individual = new IndividualModel(<IndividualModel>{ individualKey: 'some-key' });