Skip to content

Commit 487bd78

Browse files
authored
Merge pull request #31704 from microsoft/fixThisIndexSignatureAssignment
Fix assignment to property of 'this' though index signature
2 parents 7dc1f40 + d99b73c commit 487bd78

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20192,7 +20192,7 @@ namespace ts {
2019220192
markAliasReferenced(parentSymbol, node);
2019320193
}
2019420194
if (!prop) {
20195-
const indexInfo = assignmentKind === AssignmentKind.None || !isGenericObjectType(leftType) ? getIndexInfoOfType(apparentType, IndexKind.String) : undefined;
20195+
const indexInfo = assignmentKind === AssignmentKind.None || !isGenericObjectType(leftType) || isThisTypeParameter(leftType) ? getIndexInfoOfType(apparentType, IndexKind.String) : undefined;
2019620196
if (!(indexInfo && indexInfo.type)) {
2019720197
if (isJSLiteralType(leftType)) {
2019820198
return anyType;

tests/baselines/reference/keyofAndIndexedAccess2.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,12 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23
222222
let y: ReadonlyArray<string>[K] = 'abc';
223223
}
224224

225-
// Repro from #31439
225+
// Repro from #31439 and #31691
226226

227227
export class c {
228228
[x: string]: string;
229229
constructor() {
230+
this.a = "b";
230231
this["a"] = "b";
231232
}
232233
}

tests/baselines/reference/keyofAndIndexedAccess2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ function fn4<K extends number>() {
137137
let y: ReadonlyArray<string>[K] = 'abc';
138138
}
139139

140-
// Repro from #31439
140+
// Repro from #31439 and #31691
141141

142142
export class c {
143143
[x: string]: string;
144144
constructor() {
145+
this.a = "b";
145146
this["a"] = "b";
146147
}
147148
}
@@ -245,9 +246,10 @@ function fn4() {
245246
let x = 'abc';
246247
let y = 'abc';
247248
}
248-
// Repro from #31439
249+
// Repro from #31439 and #31691
249250
export class c {
250251
constructor() {
252+
this.a = "b";
251253
this["a"] = "b";
252254
}
253255
}

tests/baselines/reference/keyofAndIndexedAccess2.symbols

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ function fn4<K extends number>() {
503503
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13))
504504
}
505505

506-
// Repro from #31439
506+
// Repro from #31439 and #31691
507507

508508
export class c {
509509
>c : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
@@ -512,6 +512,9 @@ export class c {
512512
>x : Symbol(x, Decl(keyofAndIndexedAccess2.ts, 141, 3))
513513

514514
constructor() {
515+
this.a = "b";
516+
>this : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
517+
515518
this["a"] = "b";
516519
>this : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
517520
}
@@ -520,44 +523,44 @@ export class c {
520523
// Repro from #31385
521524

522525
type Foo<T> = { [key: string]: { [K in keyof T]: K }[keyof T] };
523-
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 145, 1))
524-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9))
525-
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 149, 17))
526-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 149, 34))
527-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9))
528-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 149, 34))
529-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 149, 9))
526+
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 146, 1))
527+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 150, 9))
528+
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 150, 17))
529+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 150, 34))
530+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 150, 9))
531+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 150, 34))
532+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 150, 9))
530533

531534
type Bar<T> = { [key: string]: { [K in keyof T]: [K] }[keyof T] };
532-
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 149, 64))
533-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9))
534-
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 151, 17))
535-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 151, 34))
536-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9))
537-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 151, 34))
538-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 151, 9))
535+
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 150, 64))
536+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 152, 9))
537+
>key : Symbol(key, Decl(keyofAndIndexedAccess2.ts, 152, 17))
538+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 152, 34))
539+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 152, 9))
540+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 152, 34))
541+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 152, 9))
539542

540543
type Baz<T, Q extends Foo<T>> = { [K in keyof Q]: T[Q[K]] };
541-
>Baz : Symbol(Baz, Decl(keyofAndIndexedAccess2.ts, 151, 66))
542-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9))
543-
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11))
544-
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 145, 1))
545-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9))
546-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 153, 35))
547-
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11))
548-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 153, 9))
549-
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 153, 11))
550-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 153, 35))
544+
>Baz : Symbol(Baz, Decl(keyofAndIndexedAccess2.ts, 152, 66))
545+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 154, 9))
546+
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 154, 11))
547+
>Foo : Symbol(Foo, Decl(keyofAndIndexedAccess2.ts, 146, 1))
548+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 154, 9))
549+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 154, 35))
550+
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 154, 11))
551+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 154, 9))
552+
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 154, 11))
553+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 154, 35))
551554

552555
type Qux<T, Q extends Bar<T>> = { [K in keyof Q]: T[Q[K]["0"]] };
553-
>Qux : Symbol(Qux, Decl(keyofAndIndexedAccess2.ts, 153, 60))
554-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9))
555-
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11))
556-
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 149, 64))
557-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9))
558-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 155, 35))
559-
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11))
560-
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 155, 9))
561-
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 155, 11))
562-
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 155, 35))
556+
>Qux : Symbol(Qux, Decl(keyofAndIndexedAccess2.ts, 154, 60))
557+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 156, 9))
558+
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 156, 11))
559+
>Bar : Symbol(Bar, Decl(keyofAndIndexedAccess2.ts, 150, 64))
560+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 156, 9))
561+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 156, 35))
562+
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 156, 11))
563+
>T : Symbol(T, Decl(keyofAndIndexedAccess2.ts, 156, 9))
564+
>Q : Symbol(Q, Decl(keyofAndIndexedAccess2.ts, 156, 11))
565+
>K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 156, 35))
563566

tests/baselines/reference/keyofAndIndexedAccess2.types

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ function fn4<K extends number>() {
499499
>'abc' : "abc"
500500
}
501501

502-
// Repro from #31439
502+
// Repro from #31439 and #31691
503503

504504
export class c {
505505
>c : c
@@ -508,6 +508,13 @@ export class c {
508508
>x : string
509509

510510
constructor() {
511+
this.a = "b";
512+
>this.a = "b" : "b"
513+
>this.a : string
514+
>this : this
515+
>a : string
516+
>"b" : "b"
517+
511518
this["a"] = "b";
512519
>this["a"] = "b" : "b"
513520
>this["a"] : string

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ function fn4<K extends number>() {
139139
let y: ReadonlyArray<string>[K] = 'abc';
140140
}
141141

142-
// Repro from #31439
142+
// Repro from #31439 and #31691
143143

144144
export class c {
145145
[x: string]: string;
146146
constructor() {
147+
this.a = "b";
147148
this["a"] = "b";
148149
}
149150
}

0 commit comments

Comments
 (0)