Merge pull request #8821 from Microsoft/intersectionTypeInference

Improve intersection type inference
This commit is contained in:
Anders Hejlsberg 2016-05-25 11:51:12 -07:00
commit b3f167f7a5
5 changed files with 102 additions and 5 deletions

View file

@ -7158,11 +7158,10 @@ namespace ts {
inferFromTypes(source, t);
}
}
// Next, if target is a union type containing a single naked type parameter, make a
// secondary inference to that type parameter. We don't do this for intersection types
// because in a target type like Foo & T we don't know how which parts of the source type
// should be matched by Foo and which should be inferred to T.
if (target.flags & TypeFlags.Union && typeParameterCount === 1) {
// Next, if target containings a single naked type parameter, make a secondary inference to that type
// parameter. This gives meaningful results for union types in co-variant positions and intersection
// types in contra-variant positions (such as callback parameters).
if (typeParameterCount === 1) {
inferiority++;
inferFromTypes(source, typeParameter);
inferiority--;

View file

@ -0,0 +1,17 @@
//// [intersectionTypeInference1.ts]
// Repro from #8801
function alert(s: string) {}
const parameterFn = (props:{store:string}) => alert(props.store)
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
export const Form3 = brokenFunction(parameterFn)({store: "hello"})
//// [intersectionTypeInference1.js]
// Repro from #8801
"use strict";
function alert(s) { }
var parameterFn = function (props) { return alert(props.store); };
var brokenFunction = function (f) { return function (o) { return o; }; };
exports.Form3 = brokenFunction(parameterFn)({ store: "hello" });

View file

@ -0,0 +1,33 @@
=== tests/cases/compiler/intersectionTypeInference1.ts ===
// Repro from #8801
function alert(s: string) {}
>alert : Symbol(alert, Decl(intersectionTypeInference1.ts, 0, 0))
>s : Symbol(s, Decl(intersectionTypeInference1.ts, 2, 15))
const parameterFn = (props:{store:string}) => alert(props.store)
>parameterFn : Symbol(parameterFn, Decl(intersectionTypeInference1.ts, 4, 5))
>props : Symbol(props, Decl(intersectionTypeInference1.ts, 4, 21))
>store : Symbol(store, Decl(intersectionTypeInference1.ts, 4, 28))
>alert : Symbol(alert, Decl(intersectionTypeInference1.ts, 0, 0))
>props.store : Symbol(store, Decl(intersectionTypeInference1.ts, 4, 28))
>props : Symbol(props, Decl(intersectionTypeInference1.ts, 4, 21))
>store : Symbol(store, Decl(intersectionTypeInference1.ts, 4, 28))
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
>brokenFunction : Symbol(brokenFunction, Decl(intersectionTypeInference1.ts, 5, 5))
>OwnProps : Symbol(OwnProps, Decl(intersectionTypeInference1.ts, 5, 24))
>f : Symbol(f, Decl(intersectionTypeInference1.ts, 5, 34))
>p : Symbol(p, Decl(intersectionTypeInference1.ts, 5, 38))
>dispatch : Symbol(dispatch, Decl(intersectionTypeInference1.ts, 5, 42))
>OwnProps : Symbol(OwnProps, Decl(intersectionTypeInference1.ts, 5, 24))
>o : Symbol(o, Decl(intersectionTypeInference1.ts, 5, 85))
>OwnProps : Symbol(OwnProps, Decl(intersectionTypeInference1.ts, 5, 24))
>o : Symbol(o, Decl(intersectionTypeInference1.ts, 5, 85))
export const Form3 = brokenFunction(parameterFn)({store: "hello"})
>Form3 : Symbol(Form3, Decl(intersectionTypeInference1.ts, 6, 12))
>brokenFunction : Symbol(brokenFunction, Decl(intersectionTypeInference1.ts, 5, 5))
>parameterFn : Symbol(parameterFn, Decl(intersectionTypeInference1.ts, 4, 5))
>store : Symbol(store, Decl(intersectionTypeInference1.ts, 6, 50))

View file

@ -0,0 +1,41 @@
=== tests/cases/compiler/intersectionTypeInference1.ts ===
// Repro from #8801
function alert(s: string) {}
>alert : (s: string) => void
>s : string
const parameterFn = (props:{store:string}) => alert(props.store)
>parameterFn : (props: { store: string; }) => void
>(props:{store:string}) => alert(props.store) : (props: { store: string; }) => void
>props : { store: string; }
>store : string
>alert(props.store) : void
>alert : (s: string) => void
>props.store : string
>props : { store: string; }
>store : string
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
>brokenFunction : <OwnProps>(f: (p: { dispatch: number; } & OwnProps) => void) => (o: OwnProps) => OwnProps
><OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o : <OwnProps>(f: (p: { dispatch: number; } & OwnProps) => void) => (o: OwnProps) => OwnProps
>OwnProps : OwnProps
>f : (p: { dispatch: number; } & OwnProps) => void
>p : { dispatch: number; } & OwnProps
>dispatch : number
>OwnProps : OwnProps
>(o: OwnProps) => o : (o: OwnProps) => OwnProps
>o : OwnProps
>OwnProps : OwnProps
>o : OwnProps
export const Form3 = brokenFunction(parameterFn)({store: "hello"})
>Form3 : { store: string; }
>brokenFunction(parameterFn)({store: "hello"}) : { store: string; }
>brokenFunction(parameterFn) : (o: { store: string; }) => { store: string; }
>brokenFunction : <OwnProps>(f: (p: { dispatch: number; } & OwnProps) => void) => (o: OwnProps) => OwnProps
>parameterFn : (props: { store: string; }) => void
>{store: "hello"} : { store: string; }
>store : string
>"hello" : string

View file

@ -0,0 +1,7 @@
// Repro from #8801
function alert(s: string) {}
const parameterFn = (props:{store:string}) => alert(props.store)
const brokenFunction = <OwnProps>(f: (p: {dispatch: number} & OwnProps) => void) => (o: OwnProps) => o
export const Form3 = brokenFunction(parameterFn)({store: "hello"})