typescript 打字稿中const和readonly的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46561155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:48:00  来源:igfitidea点击:

Difference between const and readonly in typescript

typescript

提问by Aravind

Constant vs readonly in typescript

打字稿中的常量与只读

Declaring a variable as readonlywill not allow us to override even if they are public properties.

声明一个变量 asreadonly不允许我们覆盖,即使它们是公共属性。

How const behaves,

const 的行为方式,

const SOME_VARIABLE:number = 10;

If I override its value, how will it work?

如果我覆盖它的值,它将如何工作?

回答by Fenton

A constvariable cannot be re-assigned, just like a readonlyproperty.

一个const变量不能被重新分配,就像一个readonly属性。

Essentially, when you define a property, you can use readonlyto prevent re-assignment. This is actually only a compile-time check.

本质上,当您定义一个属性时,您可以使用它readonly来防止重新分配。这实际上只是一个编译时检查。

When you define a constvariable (and target a more recent version of JavaScript to preserve constin the output), the check is also made at runtime.

当您定义一个const变量(并以更新版本的 JavaScript 为目标以保留const在输出中)时,也会在运行时进行检查。

So they effectively both do the same thing, but one is for variables and the other is for properties.

所以它们实际上都做同样的事情,但一个用于变量,另一个用于属性。

const x = 5;

// Not allowed
x = 7;


class Example {
    public readonly y = 6;
}

var e = new Example();

// Not allowed
e.y = 4;

Important note... "cannot be re-assigned" is not the same as immutability.

重要说明......“不能重新分配”与不变性不同。

const myArr = [1, 2, 3];

// Not allowed
myArr = [4, 5, 6]

// Perfectly fine
myArr.push(4);

// Perfectly fine
myArr[0] = 9;

回答by niranjan harpale

One of the key difference between const and readonly is in how it works with the array. (appart form already ans diff) You have to use ReadonlyArray while working with Array, where T is generic type(google it for more).

const 和 readonly 之间的主要区别之一在于它如何处理数组。(appart form已经ans diff)你必须在使用Array时使用ReadonlyArray,其中T是泛型类型(google it for more)。

when u declare any array as const, you can perform operations on array which may change the array elements. for ex.

当您将任何数组声明为 const 时,您可以对可能更改数组元素的数组执行操作。例如。

const Arr = [1,2,3];

Arr[0] = 10;   //OK
Arr.push(12); // OK
Arr.pop(); //Ok

//But
Arr = [4,5,6] // ERROR

But in case of ReadonlyArray you can not change the array as shown above.

但是在 ReadonlyArray 的情况下,您不能更改数组,如上所示。

arr1 : ReadonlyArray<number> = [10,11,12];

arr1.pop();    //ERROR
arr1.push(15); //ERROR
arr1[0] = 1;   //ERROR

回答by Stef

as said in https://www.typescriptlang.org/docs/handbook/interfaces.html#readonly-vs-const

https://www.typescriptlang.org/docs/handbook/interfaces.html#readonly-vs-const所述

The easiest way to remember whether to use readonly or const is to ask whether you're using it on a variable or a property. Variables use const whereas properties use readonly.

记住是使用 readonly 还是 const 的最简单方法是询问您是在变量还是属性上使用它。变量使用 const 而属性使用只读。

as it displayed in below image if you declare const in defining a property you will get an error https://i.imgur.com/cJfDqh9.png

如下图所示,如果您在定义属性时声明 const,您将收到错误 https://i.imgur.com/cJfDqh9.png