Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readonly modifier doesn't work with type aliases for tuple or array #32234

Closed
ikokostya opened this issue Jul 3, 2019 · 4 comments
Closed

Readonly modifier doesn't work with type aliases for tuple or array #32234

ikokostya opened this issue Jul 3, 2019 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ikokostya
Copy link
Contributor

ikokostya commented Jul 3, 2019

TypeScript Version: 3.5.1

Search Terms:

readonly, type alias, tuple, array

Code

type A = number[];
type T = [number, number];

type RA = readonly A;
type RT = readonly T;

Expected behavior:

There is no errors, like in the following example:

type RA = readonly number[];
type RT = readonly [number, number];

Actual behavior:

Compile errors:

test.ts:4:11 - error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.

4 type RA = readonly A;
            ~~~~~~~~

test.ts:5:11 - error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.

5 type RT = readonly T;
            ~~~~~~~~


Found 4 errors.

Playground Link: http://www.typescriptlang.org/play/index.html#code/C4TwDgpgBAglC8UB2BXAtgIwgJwNoF0BuAKFEigBUEpdVMcAaZdLbI408aAJTkWwgBDACYB7JABsQsEmR5V+QsZOkVCQA

Related Issues:

#12

@fatcerberus
Copy link

array and tuple literal types

A and T are not array or tuple literals, they're type constants. What you want is:

type RA = Readonly<A>;
type RT = Readonly<T>;

@ikokostya
Copy link
Contributor Author

In the wiki readonly modifier is proposed as new syntax for read-only arrays.

With current restrictions need to mix readonly modifier and Readonly type:

type Position = [number, number];

function takeCoordinates(coords: readonly Readonly<Position>[]): void {}

@fatcerberus
Copy link

On the page you linked it shows that you can't use it with type aliases:

let err1: readonly Set<number>; // error!
let err2: readonly Array<boolean>; // error!

let okay: readonly boolean[]; // works fine

And in your example, you would still need to repeat yourself even if this were supported:

function takeCoordinates(coords: readonly (readonly Position)[]): void {}

@sandersn sandersn added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 3, 2019
@sandersn
Copy link
Member

sandersn commented Jul 3, 2019

This is the current design. We have special syntax and semantics for these types only.
This might change in the future, but the most likely change is that readonly could be applied to any type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants