Skip to content

Commit 907664c

Browse files
authored
Merge pull request microsoft#31454 from microsoft/fixThisTypeIndexSignature
Permit assignment through index signature of 'this' type
2 parents eeba30a + 41a3f83 commit 907664c

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,7 +3563,7 @@ namespace ts {
35633563
context.approximateLength += 6;
35643564
return createKeywordTypeNode(SyntaxKind.ObjectKeyword);
35653565
}
3566-
if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType) {
3566+
if (isThisTypeParameter(type)) {
35673567
if (context.flags & NodeBuilderFlags.InObjectTypeLiteral) {
35683568
if (!context.encounteredError && !(context.flags & NodeBuilderFlags.AllowThisInObjectLiteral)) {
35693569
context.encounteredError = true;
@@ -10193,6 +10193,10 @@ namespace ts {
1019310193
return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.Index);
1019410194
}
1019510195

10196+
function isThisTypeParameter(type: Type): boolean {
10197+
return !!(type.flags & TypeFlags.TypeParameter && (<TypeParameter>type).isThisType);
10198+
}
10199+
1019610200
function getSimplifiedType(type: Type, writing: boolean): Type {
1019710201
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(<IndexedAccessType>type, writing) :
1019810202
type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(<ConditionalType>type, writing) :
@@ -16772,7 +16776,7 @@ namespace ts {
1677216776
}
1677316777

1677416778
function narrowByInKeyword(type: Type, literal: LiteralExpression, assumeTrue: boolean) {
16775-
if ((type.flags & (TypeFlags.Union | TypeFlags.Object)) || (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType)) {
16779+
if (type.flags & (TypeFlags.Union | TypeFlags.Object) || isThisTypeParameter(type)) {
1677616780
const propName = escapeLeadingUnderscores(literal.text);
1677716781
return filterType(type, t => isTypePresencePossible(t, propName, assumeTrue));
1677816782
}
@@ -20092,7 +20096,7 @@ namespace ts {
2009220096
return anyType;
2009320097
}
2009420098
if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) {
20095-
reportNonexistentProperty(right, leftType.flags & TypeFlags.TypeParameter && (leftType as TypeParameter).isThisType ? apparentType : leftType);
20099+
reportNonexistentProperty(right, isThisTypeParameter(leftType) ? apparentType : leftType);
2009620100
}
2009720101
return errorType;
2009820102
}
@@ -20496,7 +20500,7 @@ namespace ts {
2049620500

2049720501
const effectiveIndexType = isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType;
2049820502
const accessFlags = isAssignmentTarget(node) ?
20499-
AccessFlags.Writing | (isGenericObjectType(objectType) ? AccessFlags.NoIndexSignatures : 0) :
20503+
AccessFlags.Writing | (isGenericObjectType(objectType) && !isThisTypeParameter(objectType) ? AccessFlags.NoIndexSignatures : 0) :
2050020504
AccessFlags.None;
2050120505
const indexedAccessType = getIndexedAccessTypeOrUndefined(objectType, effectiveIndexType, node, accessFlags) || errorType;
2050220506
return checkIndexedAccessIndexType(indexedAccessType, node);

tests/baselines/reference/keyofAndIndexedAccess2.errors.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,13 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23
219219
let x: Array<string>[K] = 'abc';
220220
let y: ReadonlyArray<string>[K] = 'abc';
221221
}
222+
223+
// Repro from #31439
224+
225+
export class c {
226+
[x: string]: string;
227+
constructor() {
228+
this["a"] = "b";
229+
}
230+
}
222231

tests/baselines/reference/keyofAndIndexedAccess2.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ function fn4<K extends number>() {
136136
let x: Array<string>[K] = 'abc';
137137
let y: ReadonlyArray<string>[K] = 'abc';
138138
}
139+
140+
// Repro from #31439
141+
142+
export class c {
143+
[x: string]: string;
144+
constructor() {
145+
this["a"] = "b";
146+
}
147+
}
139148

140149

141150
//// [keyofAndIndexedAccess2.js]
@@ -226,3 +235,9 @@ function fn4() {
226235
let x = 'abc';
227236
let y = 'abc';
228237
}
238+
// Repro from #31439
239+
export class c {
240+
constructor() {
241+
this["a"] = "b";
242+
}
243+
}

tests/baselines/reference/keyofAndIndexedAccess2.symbols

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

506+
// Repro from #31439
507+
508+
export class c {
509+
>c : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
510+
511+
[x: string]: string;
512+
>x : Symbol(x, Decl(keyofAndIndexedAccess2.ts, 141, 3))
513+
514+
constructor() {
515+
this["a"] = "b";
516+
>this : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1))
517+
}
518+
}
519+

tests/baselines/reference/keyofAndIndexedAccess2.types

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,21 @@ function fn4<K extends number>() {
499499
>'abc' : "abc"
500500
}
501501

502+
// Repro from #31439
503+
504+
export class c {
505+
>c : c
506+
507+
[x: string]: string;
508+
>x : string
509+
510+
constructor() {
511+
this["a"] = "b";
512+
>this["a"] = "b" : "b"
513+
>this["a"] : string
514+
>this : this
515+
>"a" : "a"
516+
>"b" : "b"
517+
}
518+
}
519+

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ function fn4<K extends number>() {
138138
let x: Array<string>[K] = 'abc';
139139
let y: ReadonlyArray<string>[K] = 'abc';
140140
}
141+
142+
// Repro from #31439
143+
144+
export class c {
145+
[x: string]: string;
146+
constructor() {
147+
this["a"] = "b";
148+
}
149+
}

0 commit comments

Comments
 (0)