将参数属性简写与TypeScript

时间:2015-10-19 15:11:36

标签: typescript destructuring

修改

我记录了an issue on TypeScript's Github repo,他们正在接受PR来实施它。

在TypeScript中,当我们想要从构造函数定义中自动创建类中的属性时,我们可以利用参数属性的简写,例如:

class Person {
    constructor(public firstName : string, public lastName : number, public age : number) {

    }
}

然后,转换后的Javascript将是:

var Person = (function () {
    function Person(firstName, lastName, age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    return Person;
})();

但是如果我们想在构造函数中接收一个对象,那就像是:

interface IPerson {
    firstName : string,
    lastName : string,
    age: number
}

class Person {
    constructor(person : IPerson) {
        this.firstName = person.firstName;
        this.lastName = person.lastName;
        this.age = person.age;
    }
}

从TypeScript 1.5开始,我们可以利用解构,例如:

class Person {
    constructor({firstName, lastName, age} : {firstName: string, lastName: string, age: number}) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

问题:如何在TypeScript中组合参数属性简写和解构?

我试图在对象定义之前定义public,例如:

class Person {
    constructor(public {firstName, lastName, age} : {firstName: string, lastName: string, age: number}) {

    }
}

试图在每个变量之前定义它,例如:

class Person {
    constructor({public firstName, public lastName, public age} : {firstName: string, lastName: string, age: number}) {

    }
}

但我没有成功。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

目前没有一种方法可以做到这一点,所以最接近的方法是手工声明属性并从解构赋值中分配变量:

class Person {
    firstName: string;
    lastName: string;
    age: number;

    constructor({firstName, lastName, age} : {firstName: string, lastName: string, age: number}) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

如果你这样做......你可能只是决定接受IPerson并在构造函数中不使用解构来分配其成员。

答案 1 :(得分:3)

如果您有Object.assign的访问权限,则可以:

class PersonData {
  firstName: string
  constructor(args : PersonData) {
    Object.assign(this, args)
  }
}

class Person extends PersonData{}

但请注意,新实例将由args中的任何填充 - 您无法删除您想要使用的键。

答案 2 :(得分:1)

另一种策略是使用分配给不同名称的变量的能力。这减少了构造函数中的一次重复。

class Person {
    firstName: string;
    lastName: string;
    age: number;

    constructor(args: { firstName: string, lastName: string, age: number, }) {
        ({
            firstName: this.firstName,
            lastName: this.lastName,
            age: this.age,
        } = args);
    }
}

您还可以将构造函数中的一个定义移动到接口。

interface PersonConstructorArgs {
    firstName: string;
    lastName: string;
    age: number;
}
class Person {
    firstName: string;
    lastName: string;
    age: number;

    constructor(args: PersonConstructorArgs) {
        ({
            firstName: this.firstName,
            lastName: this.lastName,
            age: this.age,
        } = args);
    }
}

当您拥有层次结构时,这将非常有用。

interface PersonConstructorArgs {
    firstName: string;
    lastName: string;
    age: number;
}
class Person {
    firstName: string;
    lastName: string;
    age: number;

    constructor(args: PersonConstructorArgs) {
        ({
            firstName: this.firstName,
            lastName: this.lastName,
            age: this.age,
        } = args);
    }
}
interface EmployeeConstructorArgs extends PersonConstructorArgs {
    manager: Person;
}
class Employee extends Person {
    manager: Person;

    constructor(args: EmployeeConstructorArgs) {
        super(args);
        ({
            manager: this.manager,
        } = args);
    }
}