Skip to content

Commit 8b6bd41

Browse files
ajafffsandersn
andauthored
getPropertiesOfUnionOrIntersectionType: handle types with index signature (#31979)
* getPropertiesOfUnionOrIntersectionType: handle types with index signature Fixes: #31565 * fix test * more testing * fix typo in checker.ts Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
1 parent 4432178 commit 8b6bd41

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9842,8 +9842,8 @@ namespace ts {
98429842
}
98439843
}
98449844
// The properties of a union type are those that are present in all constituent types, so
9845-
// we only need to check the properties of the first type
9846-
if (type.flags & TypeFlags.Union) {
9845+
// we only need to check the properties of the first type without index signature
9846+
if (type.flags & TypeFlags.Union && !getIndexInfoOfType(current, IndexKind.String) && !getIndexInfoOfType(current, IndexKind.Number)) {
98479847
break;
98489848
}
98499849
}

tests/baselines/reference/conditionalTypes2.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(24,5): error TS23
1414
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"'.
1515
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"'.
1616
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"'.
17-
Type 'keyof B' is not assignable to type '"valueOf"'.
18-
Type 'string | number | symbol' is not assignable to type '"valueOf"'.
19-
Type 'string' is not assignable to type '"valueOf"'.
17+
Type 'keyof B' is not assignable to type 'number'.
18+
Type 'string | number | symbol' is not assignable to type 'number'.
19+
Type 'string' is not assignable to type 'number'.
2020
tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'.
2121
Types of property 'foo' are incompatible.
2222
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
7777
!!! 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"'.
7878
!!! 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"'.
7979
!!! 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"'.
80-
!!! error TS2322: Type 'keyof B' is not assignable to type '"valueOf"'.
81-
!!! error TS2322: Type 'string | number | symbol' is not assignable to type '"valueOf"'.
82-
!!! error TS2322: Type 'string' is not assignable to type '"valueOf"'.
80+
!!! error TS2322: Type 'keyof B' is not assignable to type 'number'.
81+
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number'.
82+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
8383
b = a; // Error
8484
~
8585
!!! error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'.

tests/cases/fourslash/completionEntryForUnionProperty2.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@
33
////interface One {
44
//// commonProperty: string;
55
//// commonFunction(): number;
6+
//// anotherProperty: Record<string, number>;
67
////}
78
////
89
////interface Two {
910
//// commonProperty: number;
1011
//// commonFunction(): number;
12+
//// anotherProperty: { foo: number }
1113
////}
1214
////
1315
////var x : One | Two;
1416
////
15-
////x.commonProperty./**/
17+
////x.commonProperty./*1*/;
18+
////x.anotherProperty./*2*/;
1619

1720
verify.completions({
18-
marker: "",
21+
marker: "1",
1922
exact: [
20-
{ name: "toString", text: "(method) toString(): string (+1 overload)", documentation: "Returns a string representation of a string.", },
23+
{ name: "toString", text: "(method) toString(): string (+1 overload)", documentation: "Returns a string representation of a string." },
2124
{ name: "valueOf", text: "(method) valueOf(): string | number", documentation: "Returns the primitive value of the specified object." },
25+
{ name: "toLocaleString", text: "(method) toLocaleString(): string (+1 overload)", documentation: "Returns a date converted to a string using the current locale." },
2226
],
2327
});
28+
29+
verify.completions({
30+
marker: '2',
31+
includes: { name: 'foo' }
32+
})

0 commit comments

Comments
 (0)