GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improved support for read-only arrays and tuples by ahejlsberg · Pull Request #29435 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Jan 15, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This PR improves our support for read-only arrays and tuples:

  • readonly modifier on array types: readonly T[] corresponds to ReadonlyArray<T> (similar to how T[] corresponds to Array<T>).
  • readonly modifier on tuple types: readonly [T0, T1, ...] correponds to a tuple type that derives from ReadonlyArray<T0 | T1 | ...> and has read-only element positions.
  • Support for read-only arrays and tuples in mapped types. When a mapped type specifies a +readonly modifier, read-write array and tuple types are mapped to their read-only form, and when a mapped type specifies a -readonly modifier, read-only array types and tuples are mapped to their read-write form. This means that Readonly<T> now produces read-only forms of arrays and tuples.

Some examples:

function f1(mt: [number, number], rt: readonly [number, number]) {
    mt[0] = 1;  // Ok
    rt[0] = 1;  // Error, read-only element
}

function f2(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
    ma = ra;  // Error
    ma = mt;  // Ok
    ma = rt;  // Error
    ra = ma;  // Ok
    ra = mt;  // Ok
    ra = rt;  // Ok
    mt = ma;  // Error
    mt = ra;  // Error
    mt = rt;  // Error
    rt = ma;  // Error
    rt = ra;  // Error
    rt = mt;  // Ok
}

type ReadWrite<T> = { -readonly [P in keyof T] : T[P] };

type T0 = Readonly<string[]>;  // readonly string[]
type T1 = Readonly<[number, number]>;  // readonly [number, number]
type T2 = Partial<Readonly<string[]>>;  // readonly (string | undefined)[]
type T3 = Readonly<Partial<string[]>>;  // readonly (string | undefined)[]
type T4 = ReadWrite<Required<T3>>;  // string[]

Fixes #26864.
Fixes #28540.
Fixes #28968.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

trotyl, alexrock, j-oliveras, ckknight, NN---, styfle, levenleven, Gerrit0, ulrichb, Bnaya, and 19 more reacted with thumbs up emoji trotyl, j-oliveras, Jessidhia, styfle, UselessPickles, Bnaya, aboyton, jcalz, SlurpTheo, intellild, and 2 more reacted with hooray emoji trotyl, j-oliveras, Jessidhia, aleclarson, ulrichb, Bnaya, aboyton, jcalz, lppedd, intellild, and 6 more reacted with heart emoji jcalz, Bnaya, intellild, j-oliveras, fvilante, and gustamora reacted with rocket emoji
All reactions
  • 👍 29 reactions
  • 🎉 12 reactions
  • ❤️ 16 reactions
  • 🚀 6 reactions
Anders Hejlsberg (ahejlsberg) added 6 commits January 12, 2019 08:45
@ahejlsberg
Respect readonly mapped type modifier when mapping arrays and tuples
903863a
@ahejlsberg
Support 'readonly' type modifier on array and tuple types
bb8378f
@ahejlsberg
Accept new baselines
7b00a29
@ahejlsberg
Update array/tuple relationship checking
db840f4
@ahejlsberg
Add tests
cff7874
@ahejlsberg
Accept new baselines
a7ca7f4
@ahejlsberg
Anders Hejlsberg (ahejlsberg) requested review from Daniel Rosenwasser (DanielRosenwasser) and Ryan Cavanaugh (RyanCavanaugh) January 15, 2019 23:42
j-oliveras
Jordi Oliveras Rovira (j-oliveras) reviewed Jan 16, 2019
View reviewed changes
Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 1353
},
"'readonly' type modifier is only permitted on array and typle types.": {

@j-oliveras Jordi Oliveras Rovira (j-oliveras) Jan 16, 2019

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: typle to tuple.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Jan 16, 2019

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jordi Oliveras Rovira (@j-oliveras) Oops. Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

j-oliveras reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@Igorbek Igor Oleinikov (Igorbek) mentioned this pull request Jan 16, 2019
Covariance / Contravariance Annotations #1394
Closed
sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Jan 16, 2019
View reviewed changes
Comment thread src/compiler/checker.ts Outdated
}
else if (node.operator === SyntaxKind.ReadonlyKeyword) {
if (node.type.kind !== SyntaxKind.ArrayType && node.type.kind !== SyntaxKind.TupleType) {
return grammarErrorOnFirstToken(node, Diagnostics.readonly_type_modifier_is_only_permitted_on_array_and_typle_types, tokenToString(SyntaxKind.SymbolKeyword));

@sheetalkamat Sheetal Nandi (sheetalkamat) Jan 16, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for this?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Anders Hejlsberg (ahejlsberg) added 3 commits January 16, 2019 09:46
@ahejlsberg
Fix typo
2256f91
@ahejlsberg
Add more tests
2ccc106
@ahejlsberg
Accept new baselines
b611a51
sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Jan 16, 2019
View reviewed changes
Comment thread src/compiler/parser.ts
switch (operator) {
case SyntaxKind.KeyOfKeyword:
case SyntaxKind.UniqueKeyword:
case SyntaxKind.ReadonlyKeyword:

@sheetalkamat Sheetal Nandi (sheetalkamat) Jan 16, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we need test/handling for .d.ts generation and decorator for this new typeNode kind.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weswigham reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Anders Hejlsberg (ahejlsberg) added 2 commits January 16, 2019 12:45
@ahejlsberg
Add .d.ts generation test
0eac506
@ahejlsberg
Accept new baselines
d24a63d
@dead-claudia Claudia Meadows (dead-claudia) mentioned this pull request Jan 16, 2019
Add a mutating return type operator to indicate whether a function modifies a property indirectly #29346
Closed
5 tasks done
@Jessidhia

Jessica Franco (Jessidhia) commented Jan 17, 2019

Copy link
Copy Markdown

Does this improved support for readonly arrays mean it's now possible to have readonly array/tuples as the type of a rest argument?

function f(...args: ReadonlyArray<string>) {} is currently a type error.

SlurpTheo, sergey-shandar, and crush83 reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Jan 17, 2019

Copy link
Copy Markdown
Member Author

@Kovensky Isn't in the PR currently, but I see no reason why we couldn't support it. I will fix it, it's just a minor change.

Jessidhia, j-oliveras, Cryrivers, aleclarson, SlurpTheo, mysticatea, robbiespeed, and aboyton reacted with hooray emoji
All reactions
  • 🎉 8 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Anders Hejlsberg (ahejlsberg) added 3 commits January 17, 2019 06:06
@ahejlsberg
Allow rest parameter type to be a readonly array or tuple
e581649
@ahejlsberg
Add tests
2200d35
@ahejlsberg
Accept new baselines
ef89d26
Sheetal Nandi (sheetalkamat) added a commit to microsoft/TypeScript-TmLanguage that referenced this pull request Jan 18, 2019
@sheetalkamat
Readonly modifier support from PR microsoft/TypeScript#29435
76cfeab
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jan 21, 2019
Const contexts for literal expressions #29510
Merged
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Jan 24, 2019
Design Meeting Notes, 1/18/2019 #29568
Closed
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) requested changes Jan 25, 2019
View reviewed changes

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for .d.ts emit coverage - the only .d.ts currently in this set has semantic errors in its originating file, which makes it shaky at best

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) assigned Anders Hejlsberg (ahejlsberg) Jan 25, 2019
@sheetalkamat

Sheetal Nandi (sheetalkamat) commented Jan 25, 2019

Copy link
Copy Markdown
Member

I think having decorator test is good idea too.. since we normally forget that when we enable new kind of type annotation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Jan 25, 2019

Copy link
Copy Markdown
Member
// @emitDecoratorMetadata: true
// @experimentalDecorators: true
// @declaration: true

declare const someDec: any;
class A {
  @someDec
  j: readonly string[];
  @someDec
  k: readonly [string, number];
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the readonlyArrayTuple branch January 30, 2019 23:59
@zhuravlikjb

Anton Lobov (zhuravlikjb) commented Jan 31, 2019

Copy link
Copy Markdown

Is this an intended change that a single 'readonly' is no longer parsed as a type?
Such code compiled fine before this change:

type readonly = string;
var q: readonly;

(This is not a sample from real code, just a test case.)

Seems that it is not a serious issue, but would be nice to know if this was planned, as you still may declare a type named 'readonly' but cannot use it anymore.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Jan 31, 2019

Copy link
Copy Markdown
Member Author

Anton Lobov (@zhuravlikjb) No, that was not intended. I will look at getting it fixed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Flarna Gerhard Stöbich (Flarna) mentioned this pull request Jan 31, 2019
[node] correct SlowBuffer DefinitelyTyped/DefinitelyTyped#32661
Merged
6 of 9 tasks
@ajafff

Klaus Meinhardt (ajafff) commented Feb 4, 2019

Copy link
Copy Markdown
Contributor

Using a version of TypeScript that contains this PR creates declaration files which are not compatible with older versions of TypeScript.

In case someone else has the same problem: I created a transformer to downlevel readonly array types in declaration files: https://github.com/ajafff/ts-transform-readonly-array

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@falsandtru

falsandtru (falsandtru) commented Feb 4, 2019

Copy link
Copy Markdown
Contributor

Anders Hejlsberg (@ahejlsberg) This feature leaved some large bugs (Maybe regression). Please fix them: #29442 #29702

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Feb 5, 2019

Copy link
Copy Markdown
Member Author

falsandtru (@falsandtru) Those issues are not related to this PR, but I have a fix in #29740.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mohsen1 Mohsen Azimi (mohsen1) mentioned this pull request Feb 5, 2019
readonly function arguments #29742
Closed
5 tasks done
@falsandtru

falsandtru (falsandtru) commented Feb 5, 2019

Copy link
Copy Markdown
Contributor

Indeed, I thought another PR. Anyway, thanks for fixing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@simonbuchan

Simon Buchan (simonbuchan) commented Feb 7, 2019

Copy link
Copy Markdown

I assume the extension to readonly SomeObject or readonly T is not worth the native compiler support, now that Readonly<T> can work for everything as expected? React.Ref<readonly T> such that it's assignable from Ref<U> for U extends T would be kind of neat though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Feb 13, 2019
TypeScript 3.4 meta-issue babel/babel#9502
Closed
TypeScript readonly type modifier babel/babel#9503
Closed
@ikokostya ikokostya mentioned this pull request Feb 14, 2019
ReadonlyArray with rest parameters #15972
Closed
@ulrichb ulrichb mentioned this pull request Feb 24, 2019
Generic argument type inference regression in TS 3.4 #30074
Closed
@dragomirtitian Titian Cernicova-Dragomir (dragomirtitian) mentioned this pull request Mar 8, 2019
DeepReadonly type fails to compile on typescript 3.1.1+ #29566
Closed
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Mar 8, 2019
TypeScript 3.4 Iteration Plan #30281
Closed
@felixfbecker

Felix Becker (felixfbecker) commented Mar 9, 2019

Copy link
Copy Markdown
Contributor

Is it still possible to declare a writable property with an immutable array? For example, constructable stylesheets defines document.adoptedStyleSheets as an array that is immutable and needs to be reassigned to be changed:

element.shadowRoot.adoptedStyleSheets = [...element.shadowRoot.adoptedStyleSheets, styleSheet]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jberry93 Jeffrey Berry (jberry93) mentioned this pull request Mar 10, 2019
TypeScript Computed Properties jberry93/jeffreyberry.dev#4
Open
@bjchambers Ben Chambers (bjchambers) mentioned this pull request Mar 28, 2019
readonly mapped type modifiers for Map / ReadonlyMap #30633
Open
5 tasks done
@ajafff Klaus Meinhardt (ajafff) mentioned this pull request Mar 30, 2019
3.4.1 breaks backward compatibility for users of ReadonlyArray #30662
Closed
@hasparus Piotr Monwid-Olechnowicz (hasparus) mentioned this pull request Apr 1, 2019
Upgrade to TypeScript version 3.4 codesandbox/codesandbox-client#1683
Closed
@PopGoesTheWza PopGoesTheWza (PopGoesTheWza) mentioned this pull request Apr 3, 2019
TypeError: Cannot read property 'instantiations' of undefined when use as const #30664
Closed
@ibezkrovnyi Igor Bezkrovnyi (ibezkrovnyi) mentioned this pull request Apr 12, 2019
[@types/prop-types] Add support of as const to oneOf DefinitelyTyped/DefinitelyTyped#34706
Merged
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jun 7, 2019
Regression in array destruction in TypeScript 3.3 #29731
Closed
@bengry Ben Grynhaus (bengry) mentioned this pull request Jun 11, 2019
Take a readonly array when possible mobxjs/mobx-state-tree#1314
Closed
3 tasks done
@jcalz Joe Calzaretta (jcalz) mentioned this pull request Jul 15, 2019
Playground: code formmating broken with TS3.4+ readonly notation microsoft/TypeScript-Website#32
Closed
@merlinnot Natan Sągol (merlinnot) mentioned this pull request Jul 24, 2019
error TS1005: ';' expected on tsc firebase/firebase-functions#404
Closed
@jack-williams Jack Williams (jack-williams) mentioned this pull request Aug 13, 2019
Support readonly types with inference #32812
Closed
5 tasks done
@sandersn Nathan Shively-Sanders (sandersn) mentioned this pull request Feb 3, 2020
Do not materialize optional undefined properties inside actual types #28727
Closed
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@sheetalkamat Sheetal Nandi (sheetalkamat) sheetalkamat left review comments
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) RyanCavanaugh approved these changes
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Awaiting requested review from DanielRosenwasser
+2 more reviewers
@j-oliveras Jordi Oliveras Rovira (j-oliveras) j-oliveras left review comments
@rbuckton Ron Buckton (rbuckton) rbuckton left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@ahejlsberg Anders Hejlsberg (ahejlsberg)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

12 participants

@ahejlsberg @Jessidhia @sheetalkamat @RyanCavanaugh @typescript-bot @zhuravlikjb @ajafff @falsandtru @simonbuchan @felixfbecker @rbuckton @j-oliveras
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.