Skip to content

Commit 473f406

Browse files
committed
Merge branch 'master' of https://github.com/Microsoft/TypeScript
2 parents 58c8d34 + b3f167f commit 473f406

5 files changed

Lines changed: 102 additions & 5 deletions

File tree

src/compiler/checker.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7158,11 +7158,10 @@ namespace ts {
71587158
inferFromTypes(source, t);
71597159
}
71607160
}
7161-
// Next, if target is a union type containing a single naked type parameter, make a
7162-
// secondary inference to that type parameter. We don't do this for intersection types
7163-
// because in a target type like Foo & T we don't know how which parts of the source type
7164-
// should be matched by Foo and which should be inferred to T.
7165-
if (target.flags & TypeFlags.Union && typeParameterCount === 1) {
7161+
// Next, if target containings a single naked type parameter, make a secondary inference to that type
7162+
// parameter. This gives meaningful results for union types in co-variant positions and intersection
7163+
// types in contra-variant positions (such as callback parameters).
7164+
if (typeParameterCount === 1) {
71667165
inferiority++;
71677166
inferFromTypes(source, typeParameter);
71687167
inferiority--;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [intersectionTypeInference1.ts]
2+
// Repro from #8801
3+
4+
function alert(s: string) {}
5+
6+
const parameterFn = (props:{store:string}) => alert(props.store)
7+
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
8+
export const Form3 = brokenFunction(parameterFn)({store: "hello"})
9+
10+
11+
//// [intersectionTypeInference1.js]
12+
// Repro from #8801
13+
"use strict";
14+
function alert(s) { }
15+
var parameterFn = function (props) { return alert(props.store); };
16+
var brokenFunction = function (f) { return function (o) { return o; }; };
17+
exports.Form3 = brokenFunction(parameterFn)({ store: "hello" });
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/compiler/intersectionTypeInference1.ts ===
2+
// Repro from #8801
3+
4+
function alert(s: string) {}
5+
>alert : Symbol(alert, Decl(intersectionTypeInference1.ts, 0, 0))
6+
>s : Symbol(s, Decl(intersectionTypeInference1.ts, 2, 15))
7+
8+
const parameterFn = (props:{store:string}) => alert(props.store)
9+
>parameterFn : Symbol(parameterFn, Decl(intersectionTypeInference1.ts, 4, 5))
10+
>props : Symbol(props, Decl(intersectionTypeInference1.ts, 4, 21))
11+
>store : Symbol(store, Decl(intersectionTypeInference1.ts, 4, 28))
12+
>alert : Symbol(alert, Decl(intersectionTypeInference1.ts, 0, 0))
13+
>props.store : Symbol(store, Decl(intersectionTypeInference1.ts, 4, 28))
14+
>props : Symbol(props, Decl(intersectionTypeInference1.ts, 4, 21))
15+
>store : Symbol(store, Decl(intersectionTypeInference1.ts, 4, 28))
16+
17+
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
18+
>brokenFunction : Symbol(brokenFunction, Decl(intersectionTypeInference1.ts, 5, 5))
19+
>OwnProps : Symbol(OwnProps, Decl(intersectionTypeInference1.ts, 5, 24))
20+
>f : Symbol(f, Decl(intersectionTypeInference1.ts, 5, 34))
21+
>p : Symbol(p, Decl(intersectionTypeInference1.ts, 5, 38))
22+
>dispatch : Symbol(dispatch, Decl(intersectionTypeInference1.ts, 5, 42))
23+
>OwnProps : Symbol(OwnProps, Decl(intersectionTypeInference1.ts, 5, 24))
24+
>o : Symbol(o, Decl(intersectionTypeInference1.ts, 5, 85))
25+
>OwnProps : Symbol(OwnProps, Decl(intersectionTypeInference1.ts, 5, 24))
26+
>o : Symbol(o, Decl(intersectionTypeInference1.ts, 5, 85))
27+
28+
export const Form3 = brokenFunction(parameterFn)({store: "hello"})
29+
>Form3 : Symbol(Form3, Decl(intersectionTypeInference1.ts, 6, 12))
30+
>brokenFunction : Symbol(brokenFunction, Decl(intersectionTypeInference1.ts, 5, 5))
31+
>parameterFn : Symbol(parameterFn, Decl(intersectionTypeInference1.ts, 4, 5))
32+
>store : Symbol(store, Decl(intersectionTypeInference1.ts, 6, 50))
33+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/compiler/intersectionTypeInference1.ts ===
2+
// Repro from #8801
3+
4+
function alert(s: string) {}
5+
>alert : (s: string) => void
6+
>s : string
7+
8+
const parameterFn = (props:{store:string}) => alert(props.store)
9+
>parameterFn : (props: { store: string; }) => void
10+
>(props:{store:string}) => alert(props.store) : (props: { store: string; }) => void
11+
>props : { store: string; }
12+
>store : string
13+
>alert(props.store) : void
14+
>alert : (s: string) => void
15+
>props.store : string
16+
>props : { store: string; }
17+
>store : string
18+
19+
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
20+
>brokenFunction : <OwnProps>(f: (p: { dispatch: number; } & OwnProps) => void) => (o: OwnProps) => OwnProps
21+
><OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o : <OwnProps>(f: (p: { dispatch: number; } & OwnProps) => void) => (o: OwnProps) => OwnProps
22+
>OwnProps : OwnProps
23+
>f : (p: { dispatch: number; } & OwnProps) => void
24+
>p : { dispatch: number; } & OwnProps
25+
>dispatch : number
26+
>OwnProps : OwnProps
27+
>(o: OwnProps) => o : (o: OwnProps) => OwnProps
28+
>o : OwnProps
29+
>OwnProps : OwnProps
30+
>o : OwnProps
31+
32+
export const Form3 = brokenFunction(parameterFn)({store: "hello"})
33+
>Form3 : { store: string; }
34+
>brokenFunction(parameterFn)({store: "hello"}) : { store: string; }
35+
>brokenFunction(parameterFn) : (o: { store: string; }) => { store: string; }
36+
>brokenFunction : <OwnProps>(f: (p: { dispatch: number; } & OwnProps) => void) => (o: OwnProps) => OwnProps
37+
>parameterFn : (props: { store: string; }) => void
38+
>{store: "hello"} : { store: string; }
39+
>store : string
40+
>"hello" : string
41+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Repro from #8801
2+
3+
function alert(s: string) {}
4+
5+
const parameterFn = (props:{store:string}) => alert(props.store)
6+
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
7+
export const Form3 = brokenFunction(parameterFn)({store: "hello"})

0 commit comments

Comments
 (0)