diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9f5673af3eee9..18c1f42f838fb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3563,7 +3563,7 @@ namespace ts { context.approximateLength += 6; return createKeywordTypeNode(SyntaxKind.ObjectKeyword); } - if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType) { + if (isThisTypeParameter(type)) { if (context.flags & NodeBuilderFlags.InObjectTypeLiteral) { if (!context.encounteredError && !(context.flags & NodeBuilderFlags.AllowThisInObjectLiteral)) { context.encounteredError = true; @@ -10193,6 +10193,10 @@ namespace ts { return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.Index); } + function isThisTypeParameter(type: Type): boolean { + return !!(type.flags & TypeFlags.TypeParameter && (type).isThisType); + } + function getSimplifiedType(type: Type, writing: boolean): Type { return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type, writing) : type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type, writing) : @@ -16772,7 +16776,7 @@ namespace ts { } function narrowByInKeyword(type: Type, literal: LiteralExpression, assumeTrue: boolean) { - if ((type.flags & (TypeFlags.Union | TypeFlags.Object)) || (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType)) { + if (type.flags & (TypeFlags.Union | TypeFlags.Object) || isThisTypeParameter(type)) { const propName = escapeLeadingUnderscores(literal.text); return filterType(type, t => isTypePresencePossible(t, propName, assumeTrue)); } @@ -20092,7 +20096,7 @@ namespace ts { return anyType; } if (right.escapedText && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistentProperty(right, leftType.flags & TypeFlags.TypeParameter && (leftType as TypeParameter).isThisType ? apparentType : leftType); + reportNonexistentProperty(right, isThisTypeParameter(leftType) ? apparentType : leftType); } return errorType; } @@ -20496,7 +20500,7 @@ namespace ts { const effectiveIndexType = isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType; const accessFlags = isAssignmentTarget(node) ? - AccessFlags.Writing | (isGenericObjectType(objectType) ? AccessFlags.NoIndexSignatures : 0) : + AccessFlags.Writing | (isGenericObjectType(objectType) && !isThisTypeParameter(objectType) ? AccessFlags.NoIndexSignatures : 0) : AccessFlags.None; const indexedAccessType = getIndexedAccessTypeOrUndefined(objectType, effectiveIndexType, node, accessFlags) || errorType; return checkIndexedAccessIndexType(indexedAccessType, node); diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt index 5b749df2cab36..9ebf28f1857e9 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt @@ -219,4 +219,13 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23 let x: Array[K] = 'abc'; let y: ReadonlyArray[K] = 'abc'; } + + // Repro from #31439 + + export class c { + [x: string]: string; + constructor() { + this["a"] = "b"; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.js b/tests/baselines/reference/keyofAndIndexedAccess2.js index 7bf53b9fba491..374b13b5a643a 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.js +++ b/tests/baselines/reference/keyofAndIndexedAccess2.js @@ -136,6 +136,15 @@ function fn4() { let x: Array[K] = 'abc'; let y: ReadonlyArray[K] = 'abc'; } + +// Repro from #31439 + +export class c { + [x: string]: string; + constructor() { + this["a"] = "b"; + } +} //// [keyofAndIndexedAccess2.js] @@ -226,3 +235,9 @@ function fn4() { let x = 'abc'; let y = 'abc'; } +// Repro from #31439 +export class c { + constructor() { + this["a"] = "b"; + } +} diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.symbols b/tests/baselines/reference/keyofAndIndexedAccess2.symbols index 2155451ef7497..a3412e8a47d31 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccess2.symbols @@ -503,3 +503,17 @@ function fn4() { >K : Symbol(K, Decl(keyofAndIndexedAccess2.ts, 133, 13)) } +// Repro from #31439 + +export class c { +>c : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1)) + + [x: string]: string; +>x : Symbol(x, Decl(keyofAndIndexedAccess2.ts, 141, 3)) + + constructor() { + this["a"] = "b"; +>this : Symbol(c, Decl(keyofAndIndexedAccess2.ts, 136, 1)) + } +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.types b/tests/baselines/reference/keyofAndIndexedAccess2.types index af43ed0f4eb27..b7b71ba4f9f71 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.types +++ b/tests/baselines/reference/keyofAndIndexedAccess2.types @@ -499,3 +499,21 @@ function fn4() { >'abc' : "abc" } +// Repro from #31439 + +export class c { +>c : c + + [x: string]: string; +>x : string + + constructor() { + this["a"] = "b"; +>this["a"] = "b" : "b" +>this["a"] : string +>this : this +>"a" : "a" +>"b" : "b" + } +} + diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts index 5251a83bb0611..a99c2b0a7a106 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts @@ -138,3 +138,12 @@ function fn4() { let x: Array[K] = 'abc'; let y: ReadonlyArray[K] = 'abc'; } + +// Repro from #31439 + +export class c { + [x: string]: string; + constructor() { + this["a"] = "b"; + } +}