Skip to content

Commit 9d3707d

Browse files
Merge pull request #29665 from ajafff/readonly-error-message
clarify error message for 'readonly' type operator
2 parents 8827bed + 782622f commit 9d3707d

File tree

7 files changed

+59
-45
lines changed

7 files changed

+59
-45
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30756,7 +30756,7 @@ namespace ts {
3075630756
}
3075730757
else if (node.operator === SyntaxKind.ReadonlyKeyword) {
3075830758
if (node.type.kind !== SyntaxKind.ArrayType && node.type.kind !== SyntaxKind.TupleType) {
30759-
return grammarErrorOnFirstToken(node, Diagnostics.readonly_type_modifier_is_only_permitted_on_array_and_tuple_types, tokenToString(SyntaxKind.SymbolKeyword));
30759+
return grammarErrorOnFirstToken(node, Diagnostics.readonly_type_modifier_is_only_permitted_on_array_and_tuple_literal_types, tokenToString(SyntaxKind.SymbolKeyword));
3076030760
}
3076130761
}
3076230762
}

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@
10231023
"category": "Error",
10241024
"code": 1353
10251025
},
1026-
"'readonly' type modifier is only permitted on array and tuple types.": {
1026+
"'readonly' type modifier is only permitted on array and tuple literal types.": {
10271027
"category": "Error",
10281028
"code": 1354
10291029
},

tests/baselines/reference/readonlyArraysAndTuples.errors.txt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(9,12): error TS1354: 'readonly' type modifier is only permitted on array and tuple types.
2-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(10,15): error TS1354: 'readonly' type modifier is only permitted on array and tuple types.
3-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(11,12): error TS1354: 'readonly' type modifier is only permitted on array and tuple types.
4-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(14,5): error TS2740: Type 'readonly string[]' is missing the following properties from type 'string[]': pop, push, reverse, shift, and 3 more.
5-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(16,5): error TS2740: Type 'readonly [string, string]' is missing the following properties from type 'string[]': pop, push, reverse, shift, and 3 more.
6-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(20,5): error TS2739: Type 'string[]' is missing the following properties from type '[string, string]': 0, 1
7-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(21,5): error TS2740: Type 'readonly string[]' is missing the following properties from type '[string, string]': 0, 1, pop, push, and 5 more.
8-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(22,5): error TS2740: Type 'readonly [string, string]' is missing the following properties from type '[string, string]': pop, push, reverse, shift, and 3 more.
9-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(23,5): error TS2739: Type 'string[]' is missing the following properties from type 'readonly [string, string]': 0, 1
10-
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(24,5): error TS2739: Type 'readonly string[]' is missing the following properties from type 'readonly [string, string]': 0, 1
1+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(9,12): error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
2+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(10,15): error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
3+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(11,12): error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
4+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(12,12): error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
5+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(15,5): error TS2740: Type 'readonly string[]' is missing the following properties from type 'string[]': pop, push, reverse, shift, and 3 more.
6+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(17,5): error TS2740: Type 'readonly [string, string]' is missing the following properties from type 'string[]': pop, push, reverse, shift, and 3 more.
7+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(21,5): error TS2739: Type 'string[]' is missing the following properties from type '[string, string]': 0, 1
8+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(22,5): error TS2740: Type 'readonly string[]' is missing the following properties from type '[string, string]': 0, 1, pop, push, and 5 more.
9+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(23,5): error TS2740: Type 'readonly [string, string]' is missing the following properties from type '[string, string]': pop, push, reverse, shift, and 3 more.
10+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(24,5): error TS2739: Type 'string[]' is missing the following properties from type 'readonly [string, string]': 0, 1
11+
tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(25,5): error TS2739: Type 'readonly string[]' is missing the following properties from type 'readonly [string, string]': 0, 1
1112

1213

13-
==== tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts (10 errors) ====
14+
==== tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts (11 errors) ====
1415
type T10 = string[];
1516
type T11 = Array<string>;
1617
type T12 = readonly string[];
@@ -21,13 +22,16 @@ tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts(24,5): error TS27
2122

2223
type T30 = readonly string; // Error
2324
~~~~~~~~
24-
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple types.
25+
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
2526
type T31<T> = readonly T; // Error
2627
~~~~~~~~
27-
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple types.
28+
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
2829
type T32 = readonly readonly string[]; // Error
2930
~~~~~~~~
30-
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple types.
31+
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
32+
type T33 = readonly Array<string>; // Error
33+
~~~~~~~~
34+
!!! error TS1354: 'readonly' type modifier is only permitted on array and tuple literal types.
3135

3236
function f1(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
3337
ma = ra; // Error

tests/baselines/reference/readonlyArraysAndTuples.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type T21 = readonly [number, number];
1010
type T30 = readonly string; // Error
1111
type T31<T> = readonly T; // Error
1212
type T32 = readonly readonly string[]; // Error
13+
type T33 = readonly Array<string>; // Error
1314

1415
function f1(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
1516
ma = ra; // Error
@@ -55,4 +56,5 @@ declare type T21 = readonly [number, number];
5556
declare type T30 = readonly string;
5657
declare type T31<T> = readonly T;
5758
declare type T32 = readonly readonly string[];
59+
declare type T33 = readonly Array<string>;
5860
declare function f1(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]): void;

tests/baselines/reference/readonlyArraysAndTuples.symbols

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,59 +30,63 @@ type T31<T> = readonly T; // Error
3030
type T32 = readonly readonly string[]; // Error
3131
>T32 : Symbol(T32, Decl(readonlyArraysAndTuples.ts, 9, 25))
3232

33+
type T33 = readonly Array<string>; // Error
34+
>T33 : Symbol(T33, Decl(readonlyArraysAndTuples.ts, 10, 38))
35+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
36+
3337
function f1(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
34-
>f1 : Symbol(f1, Decl(readonlyArraysAndTuples.ts, 10, 38))
35-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
36-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
37-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
38-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
38+
>f1 : Symbol(f1, Decl(readonlyArraysAndTuples.ts, 11, 34))
39+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
40+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
41+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
42+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
3943

4044
ma = ra; // Error
41-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
42-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
45+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
46+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
4347

4448
ma = mt;
45-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
46-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
49+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
50+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
4751

4852
ma = rt; // Error
49-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
50-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
53+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
54+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
5155

5256
ra = ma;
53-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
54-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
57+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
58+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
5559

5660
ra = mt;
57-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
58-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
61+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
62+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
5963

6064
ra = rt;
61-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
62-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
65+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
66+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
6367

6468
mt = ma; // Error
65-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
66-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
69+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
70+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
6771

6872
mt = ra; // Error
69-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
70-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
73+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
74+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
7175

7276
mt = rt; // Error
73-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
74-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
77+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
78+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
7579

7680
rt = ma; // Error
77-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
78-
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 12, 12))
81+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
82+
>ma : Symbol(ma, Decl(readonlyArraysAndTuples.ts, 13, 12))
7983

8084
rt = ra; // Error
81-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
82-
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 12, 25))
85+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
86+
>ra : Symbol(ra, Decl(readonlyArraysAndTuples.ts, 13, 25))
8387

8488
rt = mt;
85-
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 12, 70))
86-
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 12, 48))
89+
>rt : Symbol(rt, Decl(readonlyArraysAndTuples.ts, 13, 70))
90+
>mt : Symbol(mt, Decl(readonlyArraysAndTuples.ts, 13, 48))
8791
}
8892

tests/baselines/reference/readonlyArraysAndTuples.types

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type T31<T> = readonly T; // Error
2626
type T32 = readonly readonly string[]; // Error
2727
>T32 : readonly string[]
2828

29+
type T33 = readonly Array<string>; // Error
30+
>T33 : string[]
31+
2932
function f1(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
3033
>f1 : (ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) => void
3134
>ma : string[]

tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type T21 = readonly [number, number];
1212
type T30 = readonly string; // Error
1313
type T31<T> = readonly T; // Error
1414
type T32 = readonly readonly string[]; // Error
15+
type T33 = readonly Array<string>; // Error
1516

1617
function f1(ma: string[], ra: readonly string[], mt: [string, string], rt: readonly [string, string]) {
1718
ma = ra; // Error

0 commit comments

Comments
 (0)