TypeScript中类属性的命名约定

时间:2022-01-23 23:32:01

According to the offical style guide you should

根据你应该的官方风格指南

Avoid prefixing private properties and methods with an underscore.

避免使用下划线为私有属性和方法添加前缀。

As I come from a Java background, I usually would just use the this keyword:

由于我来自Java背景,我通常会使用this关键字:

export default class Device {
    private id: string;

    constructor(id: string) {
        this.id = id;
    }

    public get id(): string { // [ts] Duplicate identifier 'id'.
        return this.id;
    }

    public set id(value: string) { // [ts] Duplicate identifier 'id'.
        this.id = value;
    }
}

But the TypeScript compiler complains: [ts] Duplicate identifier 'id'.

但TypeScript编译器抱怨:[ts]重复标识符'id'。

Is there a convention or best practice for parameter naming in a TypeScript constructor?

在TypeScript构造函数中是否存在参数命名的约定或最佳实践?

EDIT
Sorry I missed the essential part of the code which actually causes the TS compiler error.
Using the get and set property of TypeScript produces the error.

编辑抱歉,我错过了实际导致TS编译器错误的代码的基本部分。使用TypeScript的get和set属性会产生错误。

So my updated question: Is there a way to follow the style guide and also use the get/set properties of TypeScript?

所以我更新了一个问题:有没有办法遵循样式指南并使用TypeScript的get / set属性?

2 个解决方案

#1


10  

Answer

If you want to use get and set accessors, you have to prefix the private property with underscore. In all other cases don't use it. I would say using underscore with accesors is a special case and although it's not explicitely written in Coding guidelines, it doesn't mean it's wrong. They use it in the official documentation.

如果要使用get和set访问器,则必须在私有属性前加下划线。在所有其他情况下不要使用它。我会说使用带有加速器的下划线是一种特殊情况,尽管它没有明确地用编码指南编写,但这并不意味着它是错误的。他们在官方文档中使用它。

Reason for the underscore

For start, I would like to emphasize the difference between field and property. In standard high level OOP languages like Java or C#, field is a private member which shouldn't be visible to other classes. If you want to expose it with encapsulation in mind, you should create a property.

首先,我想强调字段和属性之间的区别。在Java或C#等标准高级OOP语言中,field是一个私有成员,对其他类不应该是可见的。如果要在考虑封装的情况下公开它,则应创建一个属性。

In Java you do it this way (it is called Bean properties):

在Java中,您可以这样做(它被称为Bean属性):

private int id;

public int getId() {
    return this.id;
}

public setId(int value) {
    this.id = value;
}

Then you can access the property by calling these methods:

然后,您可以通过调用以下方法来访问该属性:

int i = device.getId();
device.setId(i);

//increment id by 1
device.setId(device.getId() + 1);

On the other hand, C# was designed so that it's much easier to use properties:

另一方面,C#的设计使得使用属性更加容易:

private int id;

public int Id {
    get {
        return this.id;
    }
    set {
        this.id = value;
    }
}

(value is always the assigned value.)

(值始终是指定的值。)

You can directly assign values to these properties or get the property values.

您可以直接为这些属性指定值或获取属性值。

int i = device.Id;
device.Id = i;

//increment id by 1
i

In plain JavaScript, there are no real fields, because the class members are always public; we simply call them properties.

在纯JavaScript中,没有真正的字段,因为类成员总是公开的;我们简单地称它们为属性。

In TypeScript, you can define "true" C#-like properties (with encapsulation). You use Accessors for that.

在TypeScript中,您可以定义“真正的”类C#属性(使用封装)。你使用Accessors。

private _id: number;

public get id(): number {
    return this._id;
}

public set id(value: number) {
    this._id = value;
}

Usage:

用法:

let i: number = device.id;
device.id = i;

//increment id by 1
device.id++;

You have to use underscore here because of two reasons:

你必须在这里使用下划线,原因有两个:

  1. In JavaScript, all class members are public. Therefore, by putting an underscore before private property, we sign, that this property (field) is private and should be accessed by it's public property only.
  2. 在JavaScript中,所有类成员都是公共的。因此,通过在私有属性之前加上下划线,我们签名,此属性(字段)是私有的,只能由它的公共属性访问。
  3. If you named both the private and the public property with same name, the JavaScript interpreter wouldn't know whether to access the private or public property. Thus you get the error you're writing about: [ts] Duplicate identifier 'id'.
  4. 如果您使用相同名称命名私有属性和公共属性,则JavaScript解释器将不知道是访问私有属性还是公共属性。因此,您得到了您正在撰写的错误:[ts]重复的标识符“id”。

#2


3  

If the question is strictly :

如果问题严格:

Is there a way to follow the [typeScript] style guide and also use the get/set properties of TypeScript?

有没有办法遵循[t​​ypeScript]样式指南并使用TypeScript的get / set属性?

Where the TypeScript Style Guide says :

TypeScript样式指南说:

Avoid prefixing private properties and methods with an underscore.

避免使用下划线为私有属性和方法添加前缀。

Then you can use the $ (dollar sign) instead of the _ (underscore) to prefix your private fields. In this way you both get rid of the [ts] Duplicate identifier 'blablabla' error while still respecting the TypeScript Style Guide.

然后,您可以使用$(美元符号)而不是_(下划线)作为私有字段的前缀。通过这种方式,您既可以摆脱[ts]重复标识符'blablabla'错误,同时仍然遵守TypeScript样式指南。

In addition, but it is just my opinion, the .$combination is more readable than the ._combination.

另外,但仅仅是我的观点,。$组合比._combination更具可读性。

#1


10  

Answer

If you want to use get and set accessors, you have to prefix the private property with underscore. In all other cases don't use it. I would say using underscore with accesors is a special case and although it's not explicitely written in Coding guidelines, it doesn't mean it's wrong. They use it in the official documentation.

如果要使用get和set访问器,则必须在私有属性前加下划线。在所有其他情况下不要使用它。我会说使用带有加速器的下划线是一种特殊情况,尽管它没有明确地用编码指南编写,但这并不意味着它是错误的。他们在官方文档中使用它。

Reason for the underscore

For start, I would like to emphasize the difference between field and property. In standard high level OOP languages like Java or C#, field is a private member which shouldn't be visible to other classes. If you want to expose it with encapsulation in mind, you should create a property.

首先,我想强调字段和属性之间的区别。在Java或C#等标准高级OOP语言中,field是一个私有成员,对其他类不应该是可见的。如果要在考虑封装的情况下公开它,则应创建一个属性。

In Java you do it this way (it is called Bean properties):

在Java中,您可以这样做(它被称为Bean属性):

private int id;

public int getId() {
    return this.id;
}

public setId(int value) {
    this.id = value;
}

Then you can access the property by calling these methods:

然后,您可以通过调用以下方法来访问该属性:

int i = device.getId();
device.setId(i);

//increment id by 1
device.setId(device.getId() + 1);

On the other hand, C# was designed so that it's much easier to use properties:

另一方面,C#的设计使得使用属性更加容易:

private int id;

public int Id {
    get {
        return this.id;
    }
    set {
        this.id = value;
    }
}

(value is always the assigned value.)

(值始终是指定的值。)

You can directly assign values to these properties or get the property values.

您可以直接为这些属性指定值或获取属性值。

int i = device.Id;
device.Id = i;

//increment id by 1
i

In plain JavaScript, there are no real fields, because the class members are always public; we simply call them properties.

在纯JavaScript中,没有真正的字段,因为类成员总是公开的;我们简单地称它们为属性。

In TypeScript, you can define "true" C#-like properties (with encapsulation). You use Accessors for that.

在TypeScript中,您可以定义“真正的”类C#属性(使用封装)。你使用Accessors。

private _id: number;

public get id(): number {
    return this._id;
}

public set id(value: number) {
    this._id = value;
}

Usage:

用法:

let i: number = device.id;
device.id = i;

//increment id by 1
device.id++;

You have to use underscore here because of two reasons:

你必须在这里使用下划线,原因有两个:

  1. In JavaScript, all class members are public. Therefore, by putting an underscore before private property, we sign, that this property (field) is private and should be accessed by it's public property only.
  2. 在JavaScript中,所有类成员都是公共的。因此,通过在私有属性之前加上下划线,我们签名,此属性(字段)是私有的,只能由它的公共属性访问。
  3. If you named both the private and the public property with same name, the JavaScript interpreter wouldn't know whether to access the private or public property. Thus you get the error you're writing about: [ts] Duplicate identifier 'id'.
  4. 如果您使用相同名称命名私有属性和公共属性,则JavaScript解释器将不知道是访问私有属性还是公共属性。因此,您得到了您正在撰写的错误:[ts]重复的标识符“id”。

#2


3  

If the question is strictly :

如果问题严格:

Is there a way to follow the [typeScript] style guide and also use the get/set properties of TypeScript?

有没有办法遵循[t​​ypeScript]样式指南并使用TypeScript的get / set属性?

Where the TypeScript Style Guide says :

TypeScript样式指南说:

Avoid prefixing private properties and methods with an underscore.

避免使用下划线为私有属性和方法添加前缀。

Then you can use the $ (dollar sign) instead of the _ (underscore) to prefix your private fields. In this way you both get rid of the [ts] Duplicate identifier 'blablabla' error while still respecting the TypeScript Style Guide.

然后,您可以使用$(美元符号)而不是_(下划线)作为私有字段的前缀。通过这种方式,您既可以摆脱[ts]重复标识符'blablabla'错误,同时仍然遵守TypeScript样式指南。

In addition, but it is just my opinion, the .$combination is more readable than the ._combination.

另外,但仅仅是我的观点,。$组合比._combination更具可读性。