ReadonlyArray在TypeScript中继承了什么类?(What class does ReadonlyArray inherit?)

编程入门 行业动态 更新时间:2024-10-28 14:23:43
ReadonlyArray在TypeScript中继承了什么类?(What class does ReadonlyArray inherit?)

我在TypeScript中有这个代码片段:

const arr: ReadonlyArray<string> = [ "123", "234" ]; arr.push("345");

TS编译器显示错误: Property 'push' does not exist on type 'ReadonlyArray<string>' 。 当然很明显我不能改变/改变这个数组,但问题是 - ReadonlyArray的基类是什么? 它是否继承自标准的Array类?

I have this code snippet in TypeScript:

const arr: ReadonlyArray<string> = [ "123", "234" ]; arr.push("345");

TS compiler shows an error:

Property 'push' does not exist on type 'ReadonlyArray<string>'.

Of course, it's clear that I cannot change/alter this array. The question is: what is the base class for ReadonlyArray? Does it inherit from standard Array class?

最满意答案

https://www.typescriptlang.org/docs/handbook/interfaces.html表明

TypeScript附带一个ReadonlyArray<T>类型,与Array<T>相同,删除了所有变异方法,因此您可以确保在创建后不更改数组

如果你想要源,请转到

https://github.com/Microsoft/TypeScript/blob/master/src/lib/es5.d.ts#L978

(其定义在当前版本的第978行)。

https://www.typescriptlang.org/docs/handbook/interfaces.html states that:

TypeScript comes with a ReadonlyArray<T> type that is the same as Array<T> with all mutating methods removed, so you can make sure you don’t change your arrays after creation

If you want the source, head over to:

https://github.com/Microsoft/TypeScript/blob/master/src/lib/es5.d.ts#L978

(its definition is on the line 978 in the current version).

更多推荐

本文发布于:2023-07-07 03:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1058626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:继承了   TypeScript   ReadonlyArray   class   inherit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!