From 0a8b773a8b51ee008f5ca0825b99d77b3dfc2cbf Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Wed, 19 Jun 2019 09:40:19 +0200 Subject: [PATCH 1/4] getPropertiesOfUnionOrIntersectionType: handle types with index signature Fixes: #31565 --- src/compiler/checker.ts | 4 ++-- .../baselines/reference/conditionalTypes2.errors.txt | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a4ad3af7d9a35..cd599c9871a2d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7597,8 +7597,8 @@ namespace ts { } } // The properties of a union type are those that are present in all constituent types, so - // we only need to check the properties of the first type - if (type.flags & TypeFlags.Union) { + // we only need to check the properties of the first type wihtout index signature + if (type.flags & TypeFlags.Union && !getIndexInfoOfType(current, IndexKind.String) && !getIndexInfoOfType(current, IndexKind.Number)) { break; } } diff --git a/tests/baselines/reference/conditionalTypes2.errors.txt b/tests/baselines/reference/conditionalTypes2.errors.txt index 4093bf46fb0ee..cf2efaf6adeec 100644 --- a/tests/baselines/reference/conditionalTypes2.errors.txt +++ b/tests/baselines/reference/conditionalTypes2.errors.txt @@ -14,9 +14,9 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(24,5): error TS23 Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. - Type 'keyof B' is not assignable to type '"valueOf"'. - Type 'string | number | symbol' is not assignable to type '"valueOf"'. - Type 'string' is not assignable to type '"valueOf"'. + Type 'keyof B' is not assignable to type 'number'. + Type 'string | number | symbol' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. Types of property 'foo' are incompatible. Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'. @@ -77,9 +77,9 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2 !!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. !!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. !!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. -!!! error TS2322: Type 'keyof B' is not assignable to type '"valueOf"'. -!!! error TS2322: Type 'string | number | symbol' is not assignable to type '"valueOf"'. -!!! error TS2322: Type 'string' is not assignable to type '"valueOf"'. +!!! error TS2322: Type 'keyof B' is not assignable to type 'number'. +!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. b = a; // Error ~ !!! error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. From 0787f4ce4257b671d08c38766860df7401702ca4 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Wed, 19 Jun 2019 10:02:13 +0200 Subject: [PATCH 2/4] fix test --- tests/cases/fourslash/completionEntryForUnionProperty2.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/cases/fourslash/completionEntryForUnionProperty2.ts b/tests/cases/fourslash/completionEntryForUnionProperty2.ts index 1702465aa3ed7..cab0880f4df7a 100644 --- a/tests/cases/fourslash/completionEntryForUnionProperty2.ts +++ b/tests/cases/fourslash/completionEntryForUnionProperty2.ts @@ -17,7 +17,8 @@ verify.completions({ marker: "", exact: [ - { name: "toString", text: "(method) toString(): string (+1 overload)", documentation: "Returns a string representation of a string.", }, + { name: "toString", text: "(method) toString(): string (+1 overload)", documentation: "Returns a string representation of a string." }, { name: "valueOf", text: "(method) valueOf(): string | number", documentation: "Returns the primitive value of the specified object." }, + { name: "toLocaleString", text: "(method) toLocaleString(): string (+1 overload)", documentation: "Returns a date converted to a string using the current locale." }, ], }); From fd9cb24ecb2700932b2932e2b938babcd2308034 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Wed, 19 Jun 2019 18:29:25 +0200 Subject: [PATCH 3/4] more testing --- .../fourslash/completionEntryForUnionProperty2.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/cases/fourslash/completionEntryForUnionProperty2.ts b/tests/cases/fourslash/completionEntryForUnionProperty2.ts index cab0880f4df7a..0c5e895439525 100644 --- a/tests/cases/fourslash/completionEntryForUnionProperty2.ts +++ b/tests/cases/fourslash/completionEntryForUnionProperty2.ts @@ -3,22 +3,30 @@ ////interface One { //// commonProperty: string; //// commonFunction(): number; +//// anotherProperty: Record; ////} //// ////interface Two { //// commonProperty: number; //// commonFunction(): number; +//// anotherProperty: { foo: number } ////} //// ////var x : One | Two; //// -////x.commonProperty./**/ +////x.commonProperty./*1*/; +////x.anotherProperty./*2*/; verify.completions({ - marker: "", + marker: "1", exact: [ { name: "toString", text: "(method) toString(): string (+1 overload)", documentation: "Returns a string representation of a string." }, { name: "valueOf", text: "(method) valueOf(): string | number", documentation: "Returns the primitive value of the specified object." }, { name: "toLocaleString", text: "(method) toLocaleString(): string (+1 overload)", documentation: "Returns a date converted to a string using the current locale." }, ], }); + +verify.completions({ + marker: '2', + includes: { name: 'foo' } +}) From 4bf3901086c75e8a1599068592fda611e22c4ae7 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 13 Mar 2020 14:05:37 -0700 Subject: [PATCH 4/4] fix typo in checker.ts --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cd599c9871a2d..5a5bdc91573ec 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7597,7 +7597,7 @@ namespace ts { } } // The properties of a union type are those that are present in all constituent types, so - // we only need to check the properties of the first type wihtout index signature + // we only need to check the properties of the first type without index signature if (type.flags & TypeFlags.Union && !getIndexInfoOfType(current, IndexKind.String) && !getIndexInfoOfType(current, IndexKind.Number)) { break; }