Skip to content

Commit a539887

Browse files
authored
Merge pull request #31150 from Microsoft/fixReadonlyIndexedAccess
Fix readonly indexed access used in indexed access type
2 parents 2d8527f + 64174b9 commit a539887

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10083,7 +10083,7 @@ namespace ts {
1008310083
error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
1008410084
return indexInfo.type;
1008510085
}
10086-
if (indexInfo.isReadonly && (accessFlags & AccessFlags.Writing || accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression)))) {
10086+
if (indexInfo.isReadonly && accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression))) {
1008710087
if (accessExpression) {
1008810088
error(accessExpression, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
1008910089
return indexInfo.type;

tests/baselines/reference/keyofAndIndexedAccess2.errors.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,15 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23
204204
function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void) {
205205
cb(param[0]);
206206
}
207+
208+
// Repro from #31149
209+
210+
function fn3<T extends ReadonlyArray<string>>(param: T, cb: (element: T[number]) => void) {
211+
cb(param[0]);
212+
}
213+
214+
function fn4<K extends number>() {
215+
let x: Array<string>[K] = 'abc';
216+
let y: ReadonlyArray<string>[K] = 'abc';
217+
}
207218

tests/baselines/reference/keyofAndIndexedAccess2.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ function fn<T extends {elements: Array<string>} | {elements: Array<number>}>(par
125125
function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void) {
126126
cb(param[0]);
127127
}
128+
129+
// Repro from #31149
130+
131+
function fn3<T extends ReadonlyArray<string>>(param: T, cb: (element: T[number]) => void) {
132+
cb(param[0]);
133+
}
134+
135+
function fn4<K extends number>() {
136+
let x: Array<string>[K] = 'abc';
137+
let y: ReadonlyArray<string>[K] = 'abc';
138+
}
128139

129140

130141
//// [keyofAndIndexedAccess2.js]
@@ -207,3 +218,11 @@ function fn(param, cb) {
207218
function fn2(param, cb) {
208219
cb(param[0]);
209220
}
221+
// Repro from #31149
222+
function fn3(param, cb) {
223+
cb(param[0]);
224+
}
225+
function fn4() {
226+
let x = 'abc';
227+
let y = 'abc';
228+
}

tests/baselines/reference/keyofAndIndexedAccess2.symbols

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,35 @@ function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void
471471
>param : Symbol(param, Decl(keyofAndIndexedAccess2.ts, 123, 38))
472472
}
473473

474+
// Repro from #31149
475+
476+
function fn3<T extends ReadonlyArray<string>>(param: T, cb: (element: T[number]) => void) {
477+
>fn3 : Symbol(fn3, Decl(keyofAndIndexedAccess2.ts, 125, 1))
478+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 129, 13))
479+
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --), Decl(lib.es2019.array.d.ts, --, --))
480+
>param : Symbol(param, Decl(keyofAndIndexedAccess2.ts, 129, 46))
481+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 129, 13))
482+
>cb : Symbol(cb, Decl(keyofAndIndexedAccess2.ts, 129, 55))
483+
>element : Symbol(element, Decl(keyofAndIndexedAccess2.ts, 129, 61))
484+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 129, 13))
485+
486+
cb(param[0]);
487+
>cb : Symbol(cb, Decl(keyofAndIndexedAccess2.ts, 129, 55))
488+
>param : Symbol(param, Decl(keyofAndIndexedAccess2.ts, 129, 46))
489+
}
490+
491+
function fn4<K extends number>() {
492+
>fn4 : Symbol(fn4, Decl(keyofAndIndexedAccess2.ts, 131, 1))
493+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13))
494+
495+
let x: Array<string>[K] = 'abc';
496+
>x : Symbol(x, Decl(keyofAndIndexedAccess2.ts, 134, 7))
497+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 2 more)
498+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13))
499+
500+
let y: ReadonlyArray<string>[K] = 'abc';
501+
>y : Symbol(y, Decl(keyofAndIndexedAccess2.ts, 135, 7))
502+
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --), Decl(lib.es2019.array.d.ts, --, --))
503+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13))
504+
}
505+

tests/baselines/reference/keyofAndIndexedAccess2.types

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,31 @@ function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void
471471
>0 : 0
472472
}
473473

474+
// Repro from #31149
475+
476+
function fn3<T extends ReadonlyArray<string>>(param: T, cb: (element: T[number]) => void) {
477+
>fn3 : <T extends readonly string[]>(param: T, cb: (element: T[number]) => void) => void
478+
>param : T
479+
>cb : (element: T[number]) => void
480+
>element : T[number]
481+
482+
cb(param[0]);
483+
>cb(param[0]) : void
484+
>cb : (element: T[number]) => void
485+
>param[0] : string
486+
>param : T
487+
>0 : 0
488+
}
489+
490+
function fn4<K extends number>() {
491+
>fn4 : <K extends number>() => void
492+
493+
let x: Array<string>[K] = 'abc';
494+
>x : string[][K]
495+
>'abc' : "abc"
496+
497+
let y: ReadonlyArray<string>[K] = 'abc';
498+
>y : readonly string[][K]
499+
>'abc' : "abc"
500+
}
501+

tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,14 @@ function fn<T extends {elements: Array<string>} | {elements: Array<number>}>(par
127127
function fn2<T extends Array<string>>(param: T, cb: (element: T[number]) => void) {
128128
cb(param[0]);
129129
}
130+
131+
// Repro from #31149
132+
133+
function fn3<T extends ReadonlyArray<string>>(param: T, cb: (element: T[number]) => void) {
134+
cb(param[0]);
135+
}
136+
137+
function fn4<K extends number>() {
138+
let x: Array<string>[K] = 'abc';
139+
let y: ReadonlyArray<string>[K] = 'abc';
140+
}

0 commit comments

Comments
 (0)