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

mark length of readonly tuple as readonly #47717

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14036,7 +14036,7 @@ namespace ts {
}
}
const fixedLength = properties.length;
const lengthSymbol = createSymbol(SymbolFlags.Property, "length" as __String);
const lengthSymbol = createSymbol(SymbolFlags.Property, "length" as __String, readonly ? CheckFlags.Readonly : 0);
if (combinedFlags & ElementFlags.Variable) {
lengthSymbol.type = numberType;
}
Expand Down
26 changes: 25 additions & 1 deletion tests/baselines/reference/tupleTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ tests/cases/compiler/tupleTypes.ts(50,1): error TS2322: Type '[number, number]'
tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is not assignable to type '[number, string]'.
Type at position 1 in source is not compatible with type at position 1 in target.
Type '{}' is not assignable to type 'string'.
tests/cases/compiler/tupleTypes.ts(57,1): error TS2322: Type '0' is not assignable to type '1'.
tests/cases/compiler/tupleTypes.ts(59,4): error TS2540: Cannot assign to 'length' because it is a read-only property.
tests/cases/compiler/tupleTypes.ts(61,4): error TS2540: Cannot assign to 'length' because it is a read-only property.
tests/cases/compiler/tupleTypes.ts(63,4): error TS2540: Cannot assign to 'length' because it is a read-only property.


==== tests/cases/compiler/tupleTypes.ts (14 errors) ====
==== tests/cases/compiler/tupleTypes.ts (18 errors) ====
var v1: []; // Error
var v2: [number];
var v3: [number, string];
Expand Down Expand Up @@ -120,4 +124,24 @@ tests/cases/compiler/tupleTypes.ts(51,1): error TS2322: Type '[number, {}]' is n
!!! error TS2322: Type '{}' is not assignable to type 'string'.
a3 = a1;
a3 = a2;

type B = Pick<[number], 'length'>;
declare const b: B;
b.length = 0; // Error
~~~~~~~~
!!! error TS2322: Type '0' is not assignable to type '1'.
declare const b1: readonly [number?];
b1.length = 0; // Error
~~~~~~
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
declare const b2: readonly [number, ...number[]];
b2.length = 0; // Error
~~~~~~
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
declare const b3: readonly number[];
b3.length = 0; // Error
~~~~~~
!!! error TS2540: Cannot assign to 'length' because it is a read-only property.
declare const b4: [number?];
b4.length = 0;

17 changes: 17 additions & 0 deletions tests/baselines/reference/tupleTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ a1 = a2; // Error
a1 = a3; // Error
a3 = a1;
a3 = a2;

type B = Pick<[number], 'length'>;
declare const b: B;
b.length = 0; // Error
declare const b1: readonly [number?];
b1.length = 0; // Error
declare const b2: readonly [number, ...number[]];
b2.length = 0; // Error
declare const b3: readonly number[];
b3.length = 0; // Error
declare const b4: [number?];
b4.length = 0;


//// [tupleTypes.js]
Expand Down Expand Up @@ -99,3 +111,8 @@ a1 = a2; // Error
a1 = a3; // Error
a3 = a1;
a3 = a2;
b.length = 0; // Error
b1.length = 0; // Error
b2.length = 0; // Error
b3.length = 0; // Error
b4.length = 0;
45 changes: 45 additions & 0 deletions tests/baselines/reference/tupleTypes.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,48 @@ a3 = a2;
>a3 : Symbol(a3, Decl(tupleTypes.ts, 45, 3))
>a2 : Symbol(a2, Decl(tupleTypes.ts, 44, 3))

type B = Pick<[number], 'length'>;
>B : Symbol(B, Decl(tupleTypes.ts, 52, 8))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))

declare const b: B;
>b : Symbol(b, Decl(tupleTypes.ts, 55, 13))
>B : Symbol(B, Decl(tupleTypes.ts, 52, 8))

b.length = 0; // Error
>b.length : Symbol(length)
>b : Symbol(b, Decl(tupleTypes.ts, 55, 13))
>length : Symbol(length)

declare const b1: readonly [number?];
>b1 : Symbol(b1, Decl(tupleTypes.ts, 57, 13))

b1.length = 0; // Error
>b1.length : Symbol(length)
>b1 : Symbol(b1, Decl(tupleTypes.ts, 57, 13))
>length : Symbol(length)

declare const b2: readonly [number, ...number[]];
>b2 : Symbol(b2, Decl(tupleTypes.ts, 59, 13))

b2.length = 0; // Error
>b2.length : Symbol(length)
>b2 : Symbol(b2, Decl(tupleTypes.ts, 59, 13))
>length : Symbol(length)

declare const b3: readonly number[];
>b3 : Symbol(b3, Decl(tupleTypes.ts, 61, 13))

b3.length = 0; // Error
>b3.length : Symbol(ReadonlyArray.length, Decl(lib.es5.d.ts, --, --))
>b3 : Symbol(b3, Decl(tupleTypes.ts, 61, 13))
>length : Symbol(ReadonlyArray.length, Decl(lib.es5.d.ts, --, --))

declare const b4: [number?];
>b4 : Symbol(b4, Decl(tupleTypes.ts, 63, 13))

b4.length = 0;
>b4.length : Symbol(length)
>b4 : Symbol(b4, Decl(tupleTypes.ts, 63, 13))
>length : Symbol(length)

53 changes: 53 additions & 0 deletions tests/baselines/reference/tupleTypes.types
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,56 @@ a3 = a2;
>a3 : [number, {}]
>a2 : [number, number]

type B = Pick<[number], 'length'>;
>B : B

declare const b: B;
>b : B

b.length = 0; // Error
>b.length = 0 : 0
>b.length : 1
>b : B
>length : 1
>0 : 0

declare const b1: readonly [number?];
>b1 : readonly [number?]

b1.length = 0; // Error
>b1.length = 0 : 0
>b1.length : any
>b1 : readonly [number?]
>length : any
>0 : 0

declare const b2: readonly [number, ...number[]];
>b2 : readonly [number, ...number[]]

b2.length = 0; // Error
>b2.length = 0 : 0
>b2.length : any
>b2 : readonly [number, ...number[]]
>length : any
>0 : 0

declare const b3: readonly number[];
>b3 : readonly number[]

b3.length = 0; // Error
>b3.length = 0 : 0
>b3.length : any
>b3 : readonly number[]
>length : any
>0 : 0

declare const b4: [number?];
>b4 : [number?]

b4.length = 0;
>b4.length = 0 : 0
>b4.length : 0 | 1
>b4 : [number?]
>length : 0 | 1
>0 : 0

12 changes: 12 additions & 0 deletions tests/cases/compiler/tupleTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ a1 = a2; // Error
a1 = a3; // Error
a3 = a1;
a3 = a2;

type B = Pick<[number], 'length'>;
declare const b: B;
b.length = 0; // Error
declare const b1: readonly [number?];
b1.length = 0; // Error
declare const b2: readonly [number, ...number[]];
b2.length = 0; // Error
declare const b3: readonly number[];
b3.length = 0; // Error
declare const b4: [number?];
b4.length = 0;