From c00ee42b1182cf89da8c4513cbc74a9cb92f34c2 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Oct 2014 20:52:36 -0700 Subject: [PATCH] Adding tests --- .../contextualSignatureInstantiation.js | 39 +++++++++ .../contextualSignatureInstantiation.types | 86 +++++++++++++++++++ .../contextualSignatureInstantiation.ts | 22 +++++ 3 files changed, 147 insertions(+) create mode 100644 tests/baselines/reference/contextualSignatureInstantiation.js create mode 100644 tests/baselines/reference/contextualSignatureInstantiation.types create mode 100644 tests/cases/conformance/types/typeRelationships/typeInference/contextualSignatureInstantiation.ts diff --git a/tests/baselines/reference/contextualSignatureInstantiation.js b/tests/baselines/reference/contextualSignatureInstantiation.js new file mode 100644 index 00000000000..fd3bd78dd01 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureInstantiation.js @@ -0,0 +1,39 @@ +//// [contextualSignatureInstantiation.ts] +// TypeScript Spec, section 4.12.2: +// If e is an expression of a function type that contains exactly one generic call signature and no other members, +// and T is a function type with exactly one non - generic call signature and no other members, then any inferences +// made for type parameters referenced by the parameters of T's call signature are fixed, and e's type is changed +// to a function type with e's call signature instantiated in the context of T�s call signature (section 3.8.5). + +declare function foo(cb: (x: number, y: string) => T): T; +declare function bar(x: T, y: U, cb: (x: T, y: U) => V): V; + +declare function f(x: number, y: string): boolean; +declare function g(x: T, y: T): T; + +var a: boolean; +var a = foo(f); // Should be boolean + +var b: number | string; +var b = foo(g); // Should be number | string +var b = bar(1, "one", g); // Should be number | string +var b = bar("one", 1, g); // Should be number | string + +var c: number; +var c = bar(1, 1, g); // Should be number + + +//// [contextualSignatureInstantiation.js] +// TypeScript Spec, section 4.12.2: +// If e is an expression of a function type that contains exactly one generic call signature and no other members, +// and T is a function type with exactly one non - generic call signature and no other members, then any inferences +// made for type parameters referenced by the parameters of T's call signature are fixed, and e's type is changed +// to a function type with e's call signature instantiated in the context of T�s call signature (section 3.8.5). +var a; +var a = foo(f); // Should be boolean +var b; +var b = foo(g); // Should be number | string +var b = bar(1, "one", g); // Should be number | string +var b = bar("one", 1, g); // Should be number | string +var c; +var c = bar(1, 1, g); // Should be number diff --git a/tests/baselines/reference/contextualSignatureInstantiation.types b/tests/baselines/reference/contextualSignatureInstantiation.types new file mode 100644 index 00000000000..225761024b6 --- /dev/null +++ b/tests/baselines/reference/contextualSignatureInstantiation.types @@ -0,0 +1,86 @@ +=== tests/cases/conformance/types/typeRelationships/typeInference/contextualSignatureInstantiation.ts === +// TypeScript Spec, section 4.12.2: +// If e is an expression of a function type that contains exactly one generic call signature and no other members, +// and T is a function type with exactly one non - generic call signature and no other members, then any inferences +// made for type parameters referenced by the parameters of T's call signature are fixed, and e's type is changed +// to a function type with e's call signature instantiated in the context of T�s call signature (section 3.8.5). + +declare function foo(cb: (x: number, y: string) => T): T; +>foo : (cb: (x: number, y: string) => T) => T +>T : T +>cb : (x: number, y: string) => T +>x : number +>y : string +>T : T +>T : T + +declare function bar(x: T, y: U, cb: (x: T, y: U) => V): V; +>bar : (x: T, y: U, cb: (x: T, y: U) => V) => V +>T : T +>U : U +>V : V +>x : T +>T : T +>y : U +>U : U +>cb : (x: T, y: U) => V +>x : T +>T : T +>y : U +>U : U +>V : V +>V : V + +declare function f(x: number, y: string): boolean; +>f : (x: number, y: string) => boolean +>x : number +>y : string + +declare function g(x: T, y: T): T; +>g : (x: T, y: T) => T +>T : T +>x : T +>T : T +>y : T +>T : T +>T : T + +var a: boolean; +>a : boolean + +var a = foo(f); // Should be boolean +>a : boolean +>foo(f) : boolean +>foo : (cb: (x: number, y: string) => T) => T +>f : (x: number, y: string) => boolean + +var b: number | string; +>b : string | number + +var b = foo(g); // Should be number | string +>b : string | number +>foo(g) : string | number +>foo : (cb: (x: number, y: string) => T) => T +>g : (x: T, y: T) => T + +var b = bar(1, "one", g); // Should be number | string +>b : string | number +>bar(1, "one", g) : string | number +>bar : (x: T, y: U, cb: (x: T, y: U) => V) => V +>g : (x: T, y: T) => T + +var b = bar("one", 1, g); // Should be number | string +>b : string | number +>bar("one", 1, g) : string | number +>bar : (x: T, y: U, cb: (x: T, y: U) => V) => V +>g : (x: T, y: T) => T + +var c: number; +>c : number + +var c = bar(1, 1, g); // Should be number +>c : number +>bar(1, 1, g) : number +>bar : (x: T, y: U, cb: (x: T, y: U) => V) => V +>g : (x: T, y: T) => T + diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/contextualSignatureInstantiation.ts b/tests/cases/conformance/types/typeRelationships/typeInference/contextualSignatureInstantiation.ts new file mode 100644 index 00000000000..b5408da606d --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/typeInference/contextualSignatureInstantiation.ts @@ -0,0 +1,22 @@ +// TypeScript Spec, section 4.12.2: +// If e is an expression of a function type that contains exactly one generic call signature and no other members, +// and T is a function type with exactly one non - generic call signature and no other members, then any inferences +// made for type parameters referenced by the parameters of T's call signature are fixed, and e's type is changed +// to a function type with e's call signature instantiated in the context of T’s call signature (section 3.8.5). + +declare function foo(cb: (x: number, y: string) => T): T; +declare function bar(x: T, y: U, cb: (x: T, y: U) => V): V; + +declare function f(x: number, y: string): boolean; +declare function g(x: T, y: T): T; + +var a: boolean; +var a = foo(f); // Should be boolean + +var b: number | string; +var b = foo(g); // Should be number | string +var b = bar(1, "one", g); // Should be number | string +var b = bar("one", 1, g); // Should be number | string + +var c: number; +var c = bar(1, 1, g); // Should be number