diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6c408623f6..3bb037d071 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20830,7 +20830,7 @@ namespace ts { let relatedInfo: Diagnostic | undefined; if (containingType.flags & TypeFlags.Union && !(containingType.flags & TypeFlags.Primitive)) { for (const subtype of (containingType as UnionType).types) { - if (!getPropertyOfType(subtype, propNode.escapedText)) { + if (!getPropertyOfType(subtype, propNode.escapedText) && !getIndexInfoOfType(subtype, IndexKind.String)) { errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(subtype)); break; } diff --git a/tests/baselines/reference/unionTypeWithIndexSignature.errors.txt b/tests/baselines/reference/unionTypeWithIndexSignature.errors.txt index 14a3ffc58a..8ad20f2106 100644 --- a/tests/baselines/reference/unionTypeWithIndexSignature.errors.txt +++ b/tests/baselines/reference/unionTypeWithIndexSignature.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(11,3): error TS2339: Property 'bar' does not exist on type 'Missing'. - Property 'bar' does not exist on type '{ [s: string]: string; }'. + Property 'bar' does not exist on type '{ foo: boolean; }'. tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(14,4): error TS2540: Cannot assign to 'foo' because it is a read-only property. tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(24,1): error TS7053: Element implicitly has an 'any' type because expression of type '1' can't be used to index type 'Both'. Property '1' does not exist on type 'Both'. @@ -22,7 +22,7 @@ tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(26,1): error m.bar ~~~ !!! error TS2339: Property 'bar' does not exist on type 'Missing'. -!!! error TS2339: Property 'bar' does not exist on type '{ [s: string]: string; }'. +!!! error TS2339: Property 'bar' does not exist on type '{ foo: boolean; }'. type RO = { foo: number } | { readonly [s: string]: string } declare var ro: RO ro.foo = 'not allowed'