Skip to content

Commit e1fe677

Browse files
committed
mark length of readonly tuple as readonly
1 parent 3e19cc8 commit e1fe677

File tree

6 files changed

+153
-2
lines changed

6 files changed

+153
-2
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14036,7 +14036,7 @@ namespace ts {
1403614036
}
1403714037
}
1403814038
const fixedLength = properties.length;
14039-
const lengthSymbol = createSymbol(SymbolFlags.Property, "length" as __String);
14039+
const lengthSymbol = createSymbol(SymbolFlags.Property, "length" as __String, readonly ? CheckFlags.Readonly : 0);
1404014040
if (combinedFlags & ElementFlags.Variable) {
1404114041
lengthSymbol.type = numberType;
1404214042
}

tests/baselines/reference/tupleTypes.errors.txt

+25-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ tests/cases/compiler/tupleTypes.ts(50,1): error TS2322: Type '[number, number]'
2323
tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is not assignable to type '[number, string]'.
2424
Type at position 1 in source is not compatible with type at position 1 in target.
2525
Type '{}' is not assignable to type 'string'.
26+
tests/cases/compiler/tupleTypes.ts(57,1): error TS2322: Type '0' is not assignable to type '1'.
27+
tests/cases/compiler/tupleTypes.ts(59,4): error TS2540: Cannot assign to 'length' because it is a read-only property.
28+
tests/cases/compiler/tupleTypes.ts(61,4): error TS2540: Cannot assign to 'length' because it is a read-only property.
29+
tests/cases/compiler/tupleTypes.ts(63,4): error TS2540: Cannot assign to 'length' because it is a read-only property.
2630

2731

28-
==== tests/cases/compiler/tupleTypes.ts (14 errors) ====
32+
==== tests/cases/compiler/tupleTypes.ts (18 errors) ====
2933
var v1: []; // Error
3034
var v2: [number];
3135
var v3: [number, string];
@@ -120,4 +124,24 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n
120124
!!! error TS2322: Type '{}' is not assignable to type 'string'.
121125
a3 = a1;
122126
a3 = a2;
127+
128+
type B = Pick<[number], 'length'>;
129+
declare const b: B;
130+
b.length = 0; // Error
131+
~~~~~~~~
132+
!!! error TS2322: Type '0' is not assignable to type '1'.
133+
declare const b1: readonly [number?];
134+
b1.length = 0; // Error
135+
~~~~~~
136+
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
137+
declare const b2: readonly [number, ...number[]];
138+
b2.length = 0; // Error
139+
~~~~~~
140+
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
141+
declare const b3: readonly number[];
142+
b3.length = 0; // Error
143+
~~~~~~
144+
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
145+
declare const b4: [number?];
146+
b4.length = 0;
123147

tests/baselines/reference/tupleTypes.js

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ a1 = a2; // Error
5252
a1 = a3; // Error
5353
a3 = a1;
5454
a3 = a2;
55+
56+
type B = Pick<[number], 'length'>;
57+
declare const b: B;
58+
b.length = 0; // Error
59+
declare const b1: readonly [number?];
60+
b1.length = 0; // Error
61+
declare const b2: readonly [number, ...number[]];
62+
b2.length = 0; // Error
63+
declare const b3: readonly number[];
64+
b3.length = 0; // Error
65+
declare const b4: [number?];
66+
b4.length = 0;
5567

5668

5769
//// [tupleTypes.js]
@@ -99,3 +111,8 @@ a1 = a2; // Error
99111
a1 = a3; // Error
100112
a3 = a1;
101113
a3 = a2;
114+
b.length = 0; // Error
115+
b1.length = 0; // Error
116+
b2.length = 0; // Error
117+
b3.length = 0; // Error
118+
b4.length = 0;

tests/baselines/reference/tupleTypes.symbols

+45
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,48 @@ a3 = a2;
184184
>a3 : Symbol(a3, Decl(tupleTypes.ts, 45, 3))
185185
>a2 : Symbol(a2, Decl(tupleTypes.ts, 44, 3))
186186

187+
type B = Pick<[number], 'length'>;
188+
>B : Symbol(B, Decl(tupleTypes.ts, 52, 8))
189+
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
190+
191+
declare const b: B;
192+
>b : Symbol(b, Decl(tupleTypes.ts, 55, 13))
193+
>B : Symbol(B, Decl(tupleTypes.ts, 52, 8))
194+
195+
b.length = 0; // Error
196+
>b.length : Symbol(length)
197+
>b : Symbol(b, Decl(tupleTypes.ts, 55, 13))
198+
>length : Symbol(length)
199+
200+
declare const b1: readonly [number?];
201+
>b1 : Symbol(b1, Decl(tupleTypes.ts, 57, 13))
202+
203+
b1.length = 0; // Error
204+
>b1.length : Symbol(length)
205+
>b1 : Symbol(b1, Decl(tupleTypes.ts, 57, 13))
206+
>length : Symbol(length)
207+
208+
declare const b2: readonly [number, ...number[]];
209+
>b2 : Symbol(b2, Decl(tupleTypes.ts, 59, 13))
210+
211+
b2.length = 0; // Error
212+
>b2.length : Symbol(length)
213+
>b2 : Symbol(b2, Decl(tupleTypes.ts, 59, 13))
214+
>length : Symbol(length)
215+
216+
declare const b3: readonly number[];
217+
>b3 : Symbol(b3, Decl(tupleTypes.ts, 61, 13))
218+
219+
b3.length = 0; // Error
220+
>b3.length : Symbol(ReadonlyArray.length, Decl(lib.es5.d.ts, --, --))
221+
>b3 : Symbol(b3, Decl(tupleTypes.ts, 61, 13))
222+
>length : Symbol(ReadonlyArray.length, Decl(lib.es5.d.ts, --, --))
223+
224+
declare const b4: [number?];
225+
>b4 : Symbol(b4, Decl(tupleTypes.ts, 63, 13))
226+
227+
b4.length = 0;
228+
>b4.length : Symbol(length)
229+
>b4 : Symbol(b4, Decl(tupleTypes.ts, 63, 13))
230+
>length : Symbol(length)
231+

tests/baselines/reference/tupleTypes.types

+53
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,56 @@ a3 = a2;
226226
>a3 : [number, {}]
227227
>a2 : [number, number]
228228

229+
type B = Pick<[number], 'length'>;
230+
>B : B
231+
232+
declare const b: B;
233+
>b : B
234+
235+
b.length = 0; // Error
236+
>b.length = 0 : 0
237+
>b.length : 1
238+
>b : B
239+
>length : 1
240+
>0 : 0
241+
242+
declare const b1: readonly [number?];
243+
>b1 : readonly [number?]
244+
245+
b1.length = 0; // Error
246+
>b1.length = 0 : 0
247+
>b1.length : any
248+
>b1 : readonly [number?]
249+
>length : any
250+
>0 : 0
251+
252+
declare const b2: readonly [number, ...number[]];
253+
>b2 : readonly [number, ...number[]]
254+
255+
b2.length = 0; // Error
256+
>b2.length = 0 : 0
257+
>b2.length : any
258+
>b2 : readonly [number, ...number[]]
259+
>length : any
260+
>0 : 0
261+
262+
declare const b3: readonly number[];
263+
>b3 : readonly number[]
264+
265+
b3.length = 0; // Error
266+
>b3.length = 0 : 0
267+
>b3.length : any
268+
>b3 : readonly number[]
269+
>length : any
270+
>0 : 0
271+
272+
declare const b4: [number?];
273+
>b4 : [number?]
274+
275+
b4.length = 0;
276+
>b4.length = 0 : 0
277+
>b4.length : 0 | 1
278+
>b4 : [number?]
279+
>length : 0 | 1
280+
>0 : 0
281+

tests/cases/compiler/tupleTypes.ts

+12
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ a1 = a2; // Error
5151
a1 = a3; // Error
5252
a3 = a1;
5353
a3 = a2;
54+
55+
type B = Pick<[number], 'length'>;
56+
declare const b: B;
57+
b.length = 0; // Error
58+
declare const b1: readonly [number?];
59+
b1.length = 0; // Error
60+
declare const b2: readonly [number, ...number[]];
61+
b2.length = 0; // Error
62+
declare const b3: readonly number[];
63+
b3.length = 0; // Error
64+
declare const b4: [number?];
65+
b4.length = 0;

0 commit comments

Comments
 (0)