Skip to content

Commit 92bc2dd

Browse files
authored
Consistently avoid pulling on the source return type when the target return type is any (microsoft#47306)
1 parent 8e5a84a commit 92bc2dd

5 files changed

+141
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17903,7 +17903,7 @@ namespace ts {
1790317903
const targetReturnType = isResolvingReturnTypeOfSignature(target) ? anyType
1790417904
: target.declaration && isJSConstructor(target.declaration) ? getDeclaredTypeOfClassOrInterface(getMergedSymbol(target.declaration.symbol))
1790517905
: getReturnTypeOfSignature(target);
17906-
if (targetReturnType === voidType) {
17906+
if (targetReturnType === voidType || targetReturnType === anyType) {
1790717907
return result;
1790817908
}
1790917909
const sourceReturnType = isResolvingReturnTypeOfSignature(source) ? anyType
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [conditionalTypeBasedContextualTypeReturnTypeWidening.ts]
2+
declare function useState1<S>(initialState: (S extends (() => any) ? never : S) | (() => S)): S; // No args
3+
declare function useState2<S>(initialState: (S extends ((...args: any[]) => any) ? never : S) | (() => S)): S; // Any args
4+
5+
const func1 = useState1(() => () => 0);
6+
const func2 = useState2(() => () => 0);
7+
8+
declare function useState3<S, T extends S>(initialState: (T extends (() => any) ? never : T) | (() => S)): S; // No args
9+
declare function useState4<S, T extends S>(initialState: (T extends ((...args: any[]) => any) ? never : T) | (() => S)): S; // Any args
10+
11+
const func3 = useState1(() => () => 0);
12+
const func4 = useState2(() => () => 0);
13+
14+
15+
//// [conditionalTypeBasedContextualTypeReturnTypeWidening.js]
16+
var func1 = useState1(function () { return function () { return 0; }; });
17+
var func2 = useState2(function () { return function () { return 0; }; });
18+
var func3 = useState1(function () { return function () { return 0; }; });
19+
var func4 = useState2(function () { return function () { return 0; }; });
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
=== tests/cases/compiler/conditionalTypeBasedContextualTypeReturnTypeWidening.ts ===
2+
declare function useState1<S>(initialState: (S extends (() => any) ? never : S) | (() => S)): S; // No args
3+
>useState1 : Symbol(useState1, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 0))
4+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 27))
5+
>initialState : Symbol(initialState, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 30))
6+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 27))
7+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 27))
8+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 27))
9+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 27))
10+
11+
declare function useState2<S>(initialState: (S extends ((...args: any[]) => any) ? never : S) | (() => S)): S; // Any args
12+
>useState2 : Symbol(useState2, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 96))
13+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 27))
14+
>initialState : Symbol(initialState, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 30))
15+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 27))
16+
>args : Symbol(args, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 57))
17+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 27))
18+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 27))
19+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 1, 27))
20+
21+
const func1 = useState1(() => () => 0);
22+
>func1 : Symbol(func1, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 3, 5))
23+
>useState1 : Symbol(useState1, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 0))
24+
25+
const func2 = useState2(() => () => 0);
26+
>func2 : Symbol(func2, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 4, 5))
27+
>useState2 : Symbol(useState2, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 96))
28+
29+
declare function useState3<S, T extends S>(initialState: (T extends (() => any) ? never : T) | (() => S)): S; // No args
30+
>useState3 : Symbol(useState3, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 4, 39))
31+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 27))
32+
>T : Symbol(T, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 29))
33+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 27))
34+
>initialState : Symbol(initialState, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 43))
35+
>T : Symbol(T, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 29))
36+
>T : Symbol(T, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 29))
37+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 27))
38+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 27))
39+
40+
declare function useState4<S, T extends S>(initialState: (T extends ((...args: any[]) => any) ? never : T) | (() => S)): S; // Any args
41+
>useState4 : Symbol(useState4, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 6, 109))
42+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 27))
43+
>T : Symbol(T, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 29))
44+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 27))
45+
>initialState : Symbol(initialState, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 43))
46+
>T : Symbol(T, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 29))
47+
>args : Symbol(args, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 70))
48+
>T : Symbol(T, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 29))
49+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 27))
50+
>S : Symbol(S, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 7, 27))
51+
52+
const func3 = useState1(() => () => 0);
53+
>func3 : Symbol(func3, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 9, 5))
54+
>useState1 : Symbol(useState1, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 0))
55+
56+
const func4 = useState2(() => () => 0);
57+
>func4 : Symbol(func4, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 10, 5))
58+
>useState2 : Symbol(useState2, Decl(conditionalTypeBasedContextualTypeReturnTypeWidening.ts, 0, 96))
59+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
=== tests/cases/compiler/conditionalTypeBasedContextualTypeReturnTypeWidening.ts ===
2+
declare function useState1<S>(initialState: (S extends (() => any) ? never : S) | (() => S)): S; // No args
3+
>useState1 : <S>(initialState: (S extends (() => any) ? never : S) | (() => S)) => S
4+
>initialState : (S extends () => any ? never : S) | (() => S)
5+
6+
declare function useState2<S>(initialState: (S extends ((...args: any[]) => any) ? never : S) | (() => S)): S; // Any args
7+
>useState2 : <S>(initialState: (S extends (...args: any[]) => any ? never : S) | (() => S)) => S
8+
>initialState : (S extends (...args: any[]) => any ? never : S) | (() => S)
9+
>args : any[]
10+
11+
const func1 = useState1(() => () => 0);
12+
>func1 : () => 0
13+
>useState1(() => () => 0) : () => 0
14+
>useState1 : <S>(initialState: (S extends () => any ? never : S) | (() => S)) => S
15+
>() => () => 0 : () => () => 0
16+
>() => 0 : () => 0
17+
>0 : 0
18+
19+
const func2 = useState2(() => () => 0);
20+
>func2 : () => 0
21+
>useState2(() => () => 0) : () => 0
22+
>useState2 : <S>(initialState: (S extends (...args: any[]) => any ? never : S) | (() => S)) => S
23+
>() => () => 0 : () => () => 0
24+
>() => 0 : () => 0
25+
>0 : 0
26+
27+
declare function useState3<S, T extends S>(initialState: (T extends (() => any) ? never : T) | (() => S)): S; // No args
28+
>useState3 : <S, T extends S>(initialState: (T extends (() => any) ? never : T) | (() => S)) => S
29+
>initialState : (T extends () => any ? never : T) | (() => S)
30+
31+
declare function useState4<S, T extends S>(initialState: (T extends ((...args: any[]) => any) ? never : T) | (() => S)): S; // Any args
32+
>useState4 : <S, T extends S>(initialState: (T extends (...args: any[]) => any ? never : T) | (() => S)) => S
33+
>initialState : (T extends (...args: any[]) => any ? never : T) | (() => S)
34+
>args : any[]
35+
36+
const func3 = useState1(() => () => 0);
37+
>func3 : () => 0
38+
>useState1(() => () => 0) : () => 0
39+
>useState1 : <S>(initialState: (S extends () => any ? never : S) | (() => S)) => S
40+
>() => () => 0 : () => () => 0
41+
>() => 0 : () => 0
42+
>0 : 0
43+
44+
const func4 = useState2(() => () => 0);
45+
>func4 : () => 0
46+
>useState2(() => () => 0) : () => 0
47+
>useState2 : <S>(initialState: (S extends (...args: any[]) => any ? never : S) | (() => S)) => S
48+
>() => () => 0 : () => () => 0
49+
>() => 0 : () => 0
50+
>0 : 0
51+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare function useState1<S>(initialState: (S extends (() => any) ? never : S) | (() => S)): S; // No args
2+
declare function useState2<S>(initialState: (S extends ((...args: any[]) => any) ? never : S) | (() => S)): S; // Any args
3+
4+
const func1 = useState1(() => () => 0);
5+
const func2 = useState2(() => () => 0);
6+
7+
declare function useState3<S, T extends S>(initialState: (T extends (() => any) ? never : T) | (() => S)): S; // No args
8+
declare function useState4<S, T extends S>(initialState: (T extends ((...args: any[]) => any) ? never : T) | (() => S)): S; // Any args
9+
10+
const func3 = useState1(() => () => 0);
11+
const func4 = useState2(() => () => 0);

0 commit comments

Comments
 (0)