diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 83d257cd3d26b..55b364c20101e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10083,7 +10083,7 @@ namespace ts { error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType)); return indexInfo.type; } - if (indexInfo.isReadonly && (accessFlags & AccessFlags.Writing || accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression)))) { + if (indexInfo.isReadonly && accessExpression && (isAssignmentTarget(accessExpression) || isDeleteTarget(accessExpression))) { if (accessExpression) { error(accessExpression, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType)); return indexInfo.type; diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt index 9a937066374ab..f4525727476a8 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt @@ -204,4 +204,15 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23 function fn2>(param: T, cb: (element: T[number]) => void) { cb(param[0]); } + + // Repro from #31149 + + function fn3>(param: T, cb: (element: T[number]) => void) { + cb(param[0]); + } + + function fn4() { + let x: Array[K] = 'abc'; + let y: ReadonlyArray[K] = 'abc'; + } \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.js b/tests/baselines/reference/keyofAndIndexedAccess2.js index 906bcd0eab3de..7bf53b9fba491 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.js +++ b/tests/baselines/reference/keyofAndIndexedAccess2.js @@ -125,6 +125,17 @@ function fn} | {elements: Array}>(par function fn2>(param: T, cb: (element: T[number]) => void) { cb(param[0]); } + +// Repro from #31149 + +function fn3>(param: T, cb: (element: T[number]) => void) { + cb(param[0]); +} + +function fn4() { + let x: Array[K] = 'abc'; + let y: ReadonlyArray[K] = 'abc'; +} //// [keyofAndIndexedAccess2.js] @@ -207,3 +218,11 @@ function fn(param, cb) { function fn2(param, cb) { cb(param[0]); } +// Repro from #31149 +function fn3(param, cb) { + cb(param[0]); +} +function fn4() { + let x = 'abc'; + let y = 'abc'; +} diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.symbols b/tests/baselines/reference/keyofAndIndexedAccess2.symbols index 24e1a8b0674e7..2155451ef7497 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccess2.symbols @@ -471,3 +471,35 @@ function fn2>(param: T, cb: (element: T[number]) => void >param : Symbol(param, Decl(keyofAndIndexedAccess2.ts, 123, 38)) } +// Repro from #31149 + +function fn3>(param: T, cb: (element: T[number]) => void) { +>fn3 : Symbol(fn3, Decl(keyofAndIndexedAccess2.ts, 125, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 129, 13)) +>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, --, --)) +>param : Symbol(param, Decl(keyofAndIndexedAccess2.ts, 129, 46)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 129, 13)) +>cb : Symbol(cb, Decl(keyofAndIndexedAccess2.ts, 129, 55)) +>element : Symbol(element, Decl(keyofAndIndexedAccess2.ts, 129, 61)) +>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 129, 13)) + + cb(param[0]); +>cb : Symbol(cb, Decl(keyofAndIndexedAccess2.ts, 129, 55)) +>param : Symbol(param, Decl(keyofAndIndexedAccess2.ts, 129, 46)) +} + +function fn4() { +>fn4 : Symbol(fn4, Decl(keyofAndIndexedAccess2.ts, 131, 1)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13)) + + let x: Array[K] = 'abc'; +>x : Symbol(x, Decl(keyofAndIndexedAccess2.ts, 134, 7)) +>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) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13)) + + let y: ReadonlyArray[K] = 'abc'; +>y : Symbol(y, Decl(keyofAndIndexedAccess2.ts, 135, 7)) +>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, --, --)) +>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13)) +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.types b/tests/baselines/reference/keyofAndIndexedAccess2.types index 6c0f3dff934d8..af43ed0f4eb27 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.types +++ b/tests/baselines/reference/keyofAndIndexedAccess2.types @@ -471,3 +471,31 @@ function fn2>(param: T, cb: (element: T[number]) => void >0 : 0 } +// Repro from #31149 + +function fn3>(param: T, cb: (element: T[number]) => void) { +>fn3 : (param: T, cb: (element: T[number]) => void) => void +>param : T +>cb : (element: T[number]) => void +>element : T[number] + + cb(param[0]); +>cb(param[0]) : void +>cb : (element: T[number]) => void +>param[0] : string +>param : T +>0 : 0 +} + +function fn4() { +>fn4 : () => void + + let x: Array[K] = 'abc'; +>x : string[][K] +>'abc' : "abc" + + let y: ReadonlyArray[K] = 'abc'; +>y : readonly string[][K] +>'abc' : "abc" +} + diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts index 356e0a42ae9b4..5251a83bb0611 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts @@ -127,3 +127,14 @@ function fn} | {elements: Array}>(par function fn2>(param: T, cb: (element: T[number]) => void) { cb(param[0]); } + +// Repro from #31149 + +function fn3>(param: T, cb: (element: T[number]) => void) { + cb(param[0]); +} + +function fn4() { + let x: Array[K] = 'abc'; + let y: ReadonlyArray[K] = 'abc'; +}