Skip to content

Commit cc551fb

Browse files
authored
Fix issue in findMatchingDiscriminantType (#1800)
1 parent 162a3f4 commit cc551fb

13 files changed

+11
-308
lines changed

internal/checker/relater.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,11 +1052,11 @@ func (c *Checker) findMatchingDiscriminantType(source *Type, target *Type, isRel
10521052
if match := c.getMatchingUnionConstituentForType(target, source); match != nil {
10531053
return match
10541054
}
1055-
discriminantProperties := c.findDiscriminantProperties(c.getPropertiesOfType(source), target)
1056-
discriminator := &TypeDiscriminator{c: c, props: discriminantProperties, isRelatedTo: isRelatedTo}
1057-
discriminated := c.discriminateTypeByDiscriminableItems(target, discriminator)
1058-
if discriminated != target {
1059-
return discriminated
1055+
if discriminantProperties := c.findDiscriminantProperties(c.getPropertiesOfType(source), target); len(discriminantProperties) != 0 {
1056+
discriminator := &TypeDiscriminator{c: c, props: discriminantProperties, isRelatedTo: isRelatedTo}
1057+
if discriminated := c.discriminateTypeByDiscriminableItems(target, discriminator); discriminated != target {
1058+
return discriminated
1059+
}
10601060
}
10611061
}
10621062
return nil

testdata/baselines/reference/submodule/compiler/jsxElementType.errors.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ jsxElementType.tsx(78,1): error TS2339: Property 'boop' does not exist on type '
2424
jsxElementType.tsx(79,1): error TS2339: Property 'my-undeclared-custom-element' does not exist on type 'JSX.IntrinsicElements'.
2525
jsxElementType.tsx(91,2): error TS2786: 'ReactNativeFlatList' cannot be used as a JSX component.
2626
Its type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not a valid JSX element type.
27-
Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '((props: any) => React18ReactNode) | (new (props: any) => Component<any, any, any>)'.
28-
Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
29-
Target signature provides too few arguments. Expected 2 or more, but got 1.
27+
Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
28+
Target signature provides too few arguments. Expected 2 or more, but got 1.
3029
jsxElementType.tsx(95,11): error TS2322: Type '{}' is not assignable to type 'LibraryManagedAttributes<T, {}>'.
3130
jsxElementType.tsx(98,2): error TS2304: Cannot find name 'Unresolved'.
3231
jsxElementType.tsx(99,2): error TS2304: Cannot find name 'Unresolved'.
@@ -177,9 +176,8 @@ jsxElementType.tsx(111,19): error TS2322: Type '{ a: string; b: string; }' is no
177176
~~~~~~~~~~~~~~~~~~~
178177
!!! error TS2786: 'ReactNativeFlatList' cannot be used as a JSX component.
179178
!!! error TS2786: Its type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not a valid JSX element type.
180-
!!! error TS2786: Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '((props: any) => React18ReactNode) | (new (props: any) => Component<any, any, any>)'.
181-
!!! error TS2786: Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
182-
!!! error TS2786: Target signature provides too few arguments. Expected 2 or more, but got 1.
179+
!!! error TS2786: Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
180+
!!! error TS2786: Target signature provides too few arguments. Expected 2 or more, but got 1.
183181

184182
// testing higher-order component compat
185183
function f1<T extends (props: {}) => React.ReactElement<any>>(Component: T) {

testdata/baselines/reference/submodule/compiler/jsxElementType.errors.txt.diff

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,7 @@
1818
Type '{ excessProp: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<RenderStringClass> & Readonly<{ children?: ReactNode; }> & Readonly<{ title: string; }>'.
1919
Property 'excessProp' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<RenderStringClass> & Readonly<{ children?: ReactNode; }> & Readonly<{ title: string; }>'.
2020
jsxElementType.tsx(78,1): error TS2339: Property 'boop' does not exist on type 'JSX.IntrinsicElements'.
21-
jsxElementType.tsx(79,1): error TS2339: Property 'my-undeclared-custom-element' does not exist on type 'JSX.IntrinsicElements'.
22-
jsxElementType.tsx(91,2): error TS2786: 'ReactNativeFlatList' cannot be used as a JSX component.
23-
Its type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not a valid JSX element type.
24-
- Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
25-
- Target signature provides too few arguments. Expected 2 or more, but got 1.
26-
+ Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '((props: any) => React18ReactNode) | (new (props: any) => Component<any, any, any>)'.
27-
+ Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
28-
+ Target signature provides too few arguments. Expected 2 or more, but got 1.
29-
jsxElementType.tsx(95,11): error TS2322: Type '{}' is not assignable to type 'LibraryManagedAttributes<T, {}>'.
30-
jsxElementType.tsx(98,2): error TS2304: Cannot find name 'Unresolved'.
31-
jsxElementType.tsx(99,2): error TS2304: Cannot find name 'Unresolved'.
32-
@@= skipped -129, +125 lines =@@
21+
@@= skipped -129, +124 lines =@@
3322
<RenderStringClass />;
3423
~~~~~~~~~~~~~~~~~
3524
!!! error TS2769: No overload matches this call.
@@ -59,16 +48,4 @@
5948
+!!! related TS2771 react16.d.ts:357:13: The last overload is declared here.
6049

6150
// Host element types still work
62-
<div />;
63-
@@= skipped -42, +38 lines =@@
64-
~~~~~~~~~~~~~~~~~~~
65-
!!! error TS2786: 'ReactNativeFlatList' cannot be used as a JSX component.
66-
!!! error TS2786: Its type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not a valid JSX element type.
67-
-!!! error TS2786: Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
68-
-!!! error TS2786: Target signature provides too few arguments. Expected 2 or more, but got 1.
69-
+!!! error TS2786: Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '((props: any) => React18ReactNode) | (new (props: any) => Component<any, any, any>)'.
70-
+!!! error TS2786: Type '(props: {}, ref: ForwardedRef<typeof ReactNativeFlatList>) => null' is not assignable to type '(props: any) => React18ReactNode'.
71-
+!!! error TS2786: Target signature provides too few arguments. Expected 2 or more, but got 1.
72-
73-
// testing higher-order component compat
74-
function f1<T extends (props: {}) => React.ReactElement<any>>(Component: T) {
51+
<div />;

testdata/baselines/reference/submodule/compiler/jsxFragmentWrongType.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
a.tsx(6,28): error TS2322: Type '{ children: () => string; }' is not assignable to type '{ children?: ReactNode; }'.
22
Types of property 'children' are incompatible.
33
Type '() => string' is not assignable to type 'ReactNode'.
4-
Type '() => string' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | ReactPortal'.
54
a.tsx(7,47): error TS2322: Type '() => string' is not assignable to type 'ReactNode'.
6-
Type '() => string' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | ReactPortal'.
75

86

97
==== a.tsx (2 errors) ====
@@ -17,10 +15,8 @@ a.tsx(7,47): error TS2322: Type '() => string' is not assignable to type 'ReactN
1715
!!! error TS2322: Type '{ children: () => string; }' is not assignable to type '{ children?: ReactNode; }'.
1816
!!! error TS2322: Types of property 'children' are incompatible.
1917
!!! error TS2322: Type '() => string' is not assignable to type 'ReactNode'.
20-
!!! error TS2322: Type '() => string' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | ReactPortal'.
2118
const jsxWithReactFragment = <React.Fragment>{test}</React.Fragment>;
2219
~~~~
2320
!!! error TS2322: Type '() => string' is not assignable to type 'ReactNode'.
24-
!!! error TS2322: Type '() => string' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | ReactPortal'.
2521
!!! related TS6212 a.tsx:7:47: Did you mean to call this expression?
2622

testdata/baselines/reference/submodule/compiler/jsxFragmentWrongType.errors.txt.diff

Lines changed: 0 additions & 23 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/potentiallyUncalledDecorators.errors.txt

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,20 @@ potentiallyUncalledDecorators.ts(35,1): error TS1329: 'noArgs' accepts too few a
33
potentiallyUncalledDecorators.ts(37,5): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
44
potentiallyUncalledDecorators.ts(38,5): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
55
potentiallyUncalledDecorators.ts(41,2): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof B'.
6-
Type 'OmniDecorator' is not assignable to type 'typeof B'.
7-
Type 'OmniDecorator' provides no match for the signature 'new (): B'.
86
potentiallyUncalledDecorators.ts(43,6): error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
97
potentiallyUncalledDecorators.ts(44,6): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
10-
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
118
potentiallyUncalledDecorators.ts(47,2): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof C'.
12-
Type 'OmniDecorator' is not assignable to type 'typeof C'.
13-
Type 'OmniDecorator' provides no match for the signature 'new (): C'.
149
potentiallyUncalledDecorators.ts(49,5): error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'?
1510
potentiallyUncalledDecorators.ts(50,5): error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'?
1611
potentiallyUncalledDecorators.ts(53,2): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof D'.
17-
Type 'OmniDecorator' is not assignable to type 'typeof D'.
18-
Type 'OmniDecorator' provides no match for the signature 'new (): D'.
1912
potentiallyUncalledDecorators.ts(55,6): error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
2013
potentiallyUncalledDecorators.ts(56,6): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
21-
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
2214
potentiallyUncalledDecorators.ts(59,2): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof E'.
23-
Type 'OmniDecorator' is not assignable to type 'typeof E'.
24-
Type 'OmniDecorator' provides no match for the signature 'new (): E'.
2515
potentiallyUncalledDecorators.ts(61,6): error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
2616
potentiallyUncalledDecorators.ts(62,6): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
27-
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
2817
potentiallyUncalledDecorators.ts(65,2): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof F'.
29-
Type 'OmniDecorator' is not assignable to type 'typeof F'.
30-
Type 'OmniDecorator' provides no match for the signature 'new (): F'.
3118
potentiallyUncalledDecorators.ts(67,6): error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
3219
potentiallyUncalledDecorators.ts(68,6): error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
33-
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
3420

3521

3622
==== potentiallyUncalledDecorators.ts (19 errors) ====
@@ -85,23 +71,18 @@ potentiallyUncalledDecorators.ts(68,6): error TS1270: Decorator function return
8571
@allRest
8672
~~~~~~~
8773
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof B'.
88-
!!! error TS1270: Type 'OmniDecorator' is not assignable to type 'typeof B'.
89-
!!! error TS1270: Type 'OmniDecorator' provides no match for the signature 'new (): B'.
9074
class B {
9175
@allRest foo: any;
9276
~~~~~~~
9377
!!! error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
9478
@allRest bar() { }
9579
~~~~~~~
9680
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
97-
!!! error TS1270: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
9881
}
9982

10083
@oneOptional
10184
~~~~~~~~~~~
10285
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof C'.
103-
!!! error TS1270: Type 'OmniDecorator' is not assignable to type 'typeof C'.
104-
!!! error TS1270: Type 'OmniDecorator' provides no match for the signature 'new (): C'.
10586
class C {
10687
@oneOptional foo: any;
10788
~~~~~~~~~~~~
@@ -114,46 +95,37 @@ potentiallyUncalledDecorators.ts(68,6): error TS1270: Decorator function return
11495
@twoOptional
11596
~~~~~~~~~~~
11697
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof D'.
117-
!!! error TS1270: Type 'OmniDecorator' is not assignable to type 'typeof D'.
118-
!!! error TS1270: Type 'OmniDecorator' provides no match for the signature 'new (): D'.
11998
class D {
12099
@twoOptional foo: any;
121100
~~~~~~~~~~~
122101
!!! error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
123102
@twoOptional bar() { }
124103
~~~~~~~~~~~
125104
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
126-
!!! error TS1270: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
127105
}
128106

129107
@threeOptional
130108
~~~~~~~~~~~~~
131109
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof E'.
132-
!!! error TS1270: Type 'OmniDecorator' is not assignable to type 'typeof E'.
133-
!!! error TS1270: Type 'OmniDecorator' provides no match for the signature 'new (): E'.
134110
class E {
135111
@threeOptional foo: any;
136112
~~~~~~~~~~~~~
137113
!!! error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
138114
@threeOptional bar() { }
139115
~~~~~~~~~~~~~
140116
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
141-
!!! error TS1270: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
142117
}
143118

144119
@oneOptionalWithRest
145120
~~~~~~~~~~~~~~~~~~~
146121
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | typeof F'.
147-
!!! error TS1270: Type 'OmniDecorator' is not assignable to type 'typeof F'.
148-
!!! error TS1270: Type 'OmniDecorator' provides no match for the signature 'new (): F'.
149122
class F {
150123
@oneOptionalWithRest foo: any;
151124
~~~~~~~~~~~~~~~~~~~
152125
!!! error TS1271: Decorator function return type is 'OmniDecorator' but is expected to be 'void' or 'any'.
153126
@oneOptionalWithRest bar() { }
154127
~~~~~~~~~~~~~~~~~~~
155128
!!! error TS1270: Decorator function return type 'OmniDecorator' is not assignable to type 'void | TypedPropertyDescriptor<() => void>'.
156-
!!! error TS1270: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
157129
}
158130

159131
@anyDec

0 commit comments

Comments
 (0)