From d5f02813f01f31eb4b72cf2f2f7d7351cb5328b2 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 8 Jan 2015 16:39:47 -0800 Subject: [PATCH] Added tests for contextual typing on parenthesized expressions, added case for tagged templates. --- .../parenthesizedContexualTyping1.js | 52 ++++ .../parenthesizedContexualTyping1.types | 289 ++++++++++++++++++ .../parenthesizedContexualTyping2.errors.txt | 86 ++++++ .../parenthesizedContexualTyping2.js | 126 ++++++++ .../parenthesizedContexualTyping3.js | 35 +++ .../parenthesizedContexualTyping3.types | 142 +++++++++ .../taggedTemplateContextualTyping1.js | 5 + .../taggedTemplateContextualTyping1.types | 9 + .../parenthesizedContexualTyping1.ts | 29 ++ .../parenthesizedContexualTyping2.ts | 35 +++ .../parenthesizedContexualTyping3.ts | 21 ++ .../taggedTemplateContextualTyping1.ts | 1 + 12 files changed, 830 insertions(+) create mode 100644 tests/baselines/reference/parenthesizedContexualTyping1.js create mode 100644 tests/baselines/reference/parenthesizedContexualTyping1.types create mode 100644 tests/baselines/reference/parenthesizedContexualTyping2.errors.txt create mode 100644 tests/baselines/reference/parenthesizedContexualTyping2.js create mode 100644 tests/baselines/reference/parenthesizedContexualTyping3.js create mode 100644 tests/baselines/reference/parenthesizedContexualTyping3.types create mode 100644 tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping1.ts create mode 100644 tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts create mode 100644 tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping3.ts diff --git a/tests/baselines/reference/parenthesizedContexualTyping1.js b/tests/baselines/reference/parenthesizedContexualTyping1.js new file mode 100644 index 00000000000..2f1e9ff62fc --- /dev/null +++ b/tests/baselines/reference/parenthesizedContexualTyping1.js @@ -0,0 +1,52 @@ +//// [parenthesizedContexualTyping1.ts] + +function fun(g: (x: T) => T, x: T): T; +function fun(g: (x: T) => T, h: (y: T) => T, x: T): T; +function fun(g: (x: T) => T, x: T): T { + return g(x); +} + +var a = fun(x => x, 10); +var b = fun((x => x), 10); +var c = fun(((x => x)), 10); +var d = fun((((x => x))), 10); + +var e = fun(x => x, x => x, 10); +var f = fun((x => x), (x => x), 10); +var g = fun(((x => x)), ((x => x)), 10); +var h = fun((((x => x))), ((x => x)), 10); + +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? x => x : x => undefined), 10); +var j = fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), 10); +var k = fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), x => x, 10); +var l = fun(((Math.random() < 0.5 ? ((x => x)) : ((x => undefined)))), ((x => x)), 10); + +var lambda1: (x: number) => number = x => x; +var lambda2: (x: number) => number = (x => x); + +type ObjType = { x: (p: number) => string; y: (p: string) => number }; +var obj1: ObjType = { x: x => (x, undefined), y: y => (y, undefined) }; +var obj2: ObjType = ({ x: x => (x, undefined), y: y => (y, undefined) }); + +//// [parenthesizedContexualTyping1.js] +function fun(g, x) { + return g(x); +} +var a = fun(function (x) { return x; }, 10); +var b = fun((function (x) { return x; }), 10); +var c = fun(((function (x) { return x; })), 10); +var d = fun((((function (x) { return x; }))), 10); +var e = fun(function (x) { return x; }, function (x) { return x; }, 10); +var f = fun((function (x) { return x; }), (function (x) { return x; }), 10); +var g = fun(((function (x) { return x; })), ((function (x) { return x; })), 10); +var h = fun((((function (x) { return x; }))), ((function (x) { return x; })), 10); +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? function (x) { return x; } : function (x) { return undefined; }), 10); +var j = fun((Math.random() < 0.5 ? (function (x) { return x; }) : (function (x) { return undefined; })), 10); +var k = fun((Math.random() < 0.5 ? (function (x) { return x; }) : (function (x) { return undefined; })), function (x) { return x; }, 10); +var l = fun(((Math.random() < 0.5 ? ((function (x) { return x; })) : ((function (x) { return undefined; })))), ((function (x) { return x; })), 10); +var lambda1 = function (x) { return x; }; +var lambda2 = (function (x) { return x; }); +var obj1 = { x: function (x) { return (x, undefined); }, y: function (y) { return (y, undefined); } }; +var obj2 = ({ x: function (x) { return (x, undefined); }, y: function (y) { return (y, undefined); } }); diff --git a/tests/baselines/reference/parenthesizedContexualTyping1.types b/tests/baselines/reference/parenthesizedContexualTyping1.types new file mode 100644 index 00000000000..c8685206c47 --- /dev/null +++ b/tests/baselines/reference/parenthesizedContexualTyping1.types @@ -0,0 +1,289 @@ +=== tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping1.ts === + +function fun(g: (x: T) => T, x: T): T; +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>T : T +>g : (x: T) => T +>x : T +>T : T +>T : T +>x : T +>T : T +>T : T + +function fun(g: (x: T) => T, h: (y: T) => T, x: T): T; +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>T : T +>g : (x: T) => T +>x : T +>T : T +>T : T +>h : (y: T) => T +>y : T +>T : T +>T : T +>x : T +>T : T +>T : T + +function fun(g: (x: T) => T, x: T): T { +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>T : T +>g : (x: T) => T +>x : T +>T : T +>T : T +>x : T +>T : T +>T : T + + return g(x); +>g(x) : T +>g : (x: T) => T +>x : T +} + +var a = fun(x => x, 10); +>a : number +>fun(x => x, 10) : number +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>x => x : (x: number) => number +>x : number +>x : number + +var b = fun((x => x), 10); +>b : any +>fun((x => x), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var c = fun(((x => x)), 10); +>c : any +>fun(((x => x)), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var d = fun((((x => x))), 10); +>d : any +>fun((((x => x))), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(((x => x))) : (x: any) => any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var e = fun(x => x, x => x, 10); +>e : number +>fun(x => x, x => x, 10) : number +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>x => x : (x: number) => number +>x : number +>x : number +>x => x : (x: number) => number +>x : number +>x : number + +var f = fun((x => x), (x => x), 10); +>f : any +>fun((x => x), (x => x), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var g = fun(((x => x)), ((x => x)), 10); +>g : any +>fun(((x => x)), ((x => x)), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var h = fun((((x => x))), ((x => x)), 10); +>h : any +>fun((((x => x))), ((x => x)), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(((x => x))) : (x: any) => any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? x => x : x => undefined), 10); +>i : any +>fun((Math.random() < 0.5 ? x => x : x => undefined), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(Math.random() < 0.5 ? x => x : x => undefined) : (x: any) => any +>Math.random() < 0.5 ? x => x : x => undefined : (x: any) => any +>Math.random() < 0.5 : boolean +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number +>x => x : (x: any) => any +>x : any +>x : any +>x => undefined : (x: any) => any +>x : any +>undefined : undefined + +var j = fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), 10); +>j : any +>fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(Math.random() < 0.5 ? (x => x) : (x => undefined)) : (x: any) => any +>Math.random() < 0.5 ? (x => x) : (x => undefined) : (x: any) => any +>Math.random() < 0.5 : boolean +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>(x => undefined) : (x: any) => any +>x => undefined : (x: any) => any +>x : any +>undefined : undefined + +var k = fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), x => x, 10); +>k : any +>fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), x => x, 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(Math.random() < 0.5 ? (x => x) : (x => undefined)) : (x: any) => any +>Math.random() < 0.5 ? (x => x) : (x => undefined) : (x: any) => any +>Math.random() < 0.5 : boolean +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>(x => undefined) : (x: any) => any +>x => undefined : (x: any) => any +>x : any +>undefined : undefined +>x => x : (x: any) => any +>x : any +>x : any + +var l = fun(((Math.random() < 0.5 ? ((x => x)) : ((x => undefined)))), ((x => x)), 10); +>l : any +>fun(((Math.random() < 0.5 ? ((x => x)) : ((x => undefined)))), ((x => x)), 10) : any +>fun : { (g: (x: T) => T, x: T): T; (g: (x: T) => T, h: (y: T) => T, x: T): T; } +>((Math.random() < 0.5 ? ((x => x)) : ((x => undefined)))) : (x: any) => any +>(Math.random() < 0.5 ? ((x => x)) : ((x => undefined))) : (x: any) => any +>Math.random() < 0.5 ? ((x => x)) : ((x => undefined)) : (x: any) => any +>Math.random() < 0.5 : boolean +>Math.random() : number +>Math.random : () => number +>Math : Math +>random : () => number +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>((x => undefined)) : (x: any) => any +>(x => undefined) : (x: any) => any +>x => undefined : (x: any) => any +>x : any +>undefined : undefined +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var lambda1: (x: number) => number = x => x; +>lambda1 : (x: number) => number +>x : number +>x => x : (x: number) => number +>x : number +>x : number + +var lambda2: (x: number) => number = (x => x); +>lambda2 : (x: number) => number +>x : number +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +type ObjType = { x: (p: number) => string; y: (p: string) => number }; +>ObjType : { x: (p: number) => string; y: (p: string) => number; } +>x : (p: number) => string +>p : number +>y : (p: string) => number +>p : string + +var obj1: ObjType = { x: x => (x, undefined), y: y => (y, undefined) }; +>obj1 : { x: (p: number) => string; y: (p: string) => number; } +>ObjType : { x: (p: number) => string; y: (p: string) => number; } +>{ x: x => (x, undefined), y: y => (y, undefined) } : { x: (x: number) => any; y: (y: string) => any; } +>x : (x: number) => any +>x => (x, undefined) : (x: number) => any +>x : number +>(x, undefined) : undefined +>x, undefined : undefined +>x : number +>undefined : undefined +>y : (y: string) => any +>y => (y, undefined) : (y: string) => any +>y : string +>(y, undefined) : undefined +>y, undefined : undefined +>y : string +>undefined : undefined + +var obj2: ObjType = ({ x: x => (x, undefined), y: y => (y, undefined) }); +>obj2 : { x: (p: number) => string; y: (p: string) => number; } +>ObjType : { x: (p: number) => string; y: (p: string) => number; } +>({ x: x => (x, undefined), y: y => (y, undefined) }) : { x: (x: any) => any; y: (y: any) => any; } +>{ x: x => (x, undefined), y: y => (y, undefined) } : { x: (x: any) => any; y: (y: any) => any; } +>x : (x: any) => any +>x => (x, undefined) : (x: any) => any +>x : any +>(x, undefined) : undefined +>x, undefined : undefined +>x : any +>undefined : undefined +>y : (y: any) => any +>y => (y, undefined) : (y: any) => any +>y : any +>(y, undefined) : undefined +>y, undefined : undefined +>y : any +>undefined : undefined + diff --git a/tests/baselines/reference/parenthesizedContexualTyping2.errors.txt b/tests/baselines/reference/parenthesizedContexualTyping2.errors.txt new file mode 100644 index 00000000000..236dfee0979 --- /dev/null +++ b/tests/baselines/reference/parenthesizedContexualTyping2.errors.txt @@ -0,0 +1,86 @@ +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(15,21): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(16,22): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(17,23): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(20,21): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(20,64): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(21,22): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(21,67): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(22,23): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(22,69): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(25,43): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(26,44): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(27,44): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(28,46): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(28,114): error TS2347: Untyped function calls may not accept type arguments. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(30,45): error TS2349: Cannot invoke an expression whose type lacks a call signature. +tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts(31,46): error TS2347: Untyped function calls may not accept type arguments. + + +==== tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts (16 errors) ==== + // These tests ensure that in cases where it may *appear* that a value has a type, + // they actually are properly being contextually typed. The way we test this is + // that we invoke contextually typed arguments with type arguments. + // Since 'any' cannot be invoked with type arguments, we should get errors back. + + type FuncType = (x: (p: T) => T) => typeof x; + + function fun(f: FuncType, x: T): T; + function fun(f: FuncType, g: FuncType, x: T): T; + function fun(...rest: any[]): T { + return undefined; + } + + var a = fun(x => { x(undefined); return x; }, 10); + var b = fun((x => { x(undefined); return x; }), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var c = fun(((x => { x(undefined); return x; })), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var d = fun((((x => { x(undefined); return x; }))), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + + var e = fun(x => { x(undefined); return x; }, x => { x(undefined); return x; }, 10); + var f = fun((x => { x(undefined); return x; }),(x => { x(undefined); return x; }), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var g = fun(((x => { x(undefined); return x; })),((x => { x(undefined); return x; })), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var h = fun((((x => { x(undefined); return x; }))),((x => { x(undefined); return x; })), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + + // Ternaries in parens + var i = fun((Math.random() < 0.5 ? x => { x(undefined); return x; } : x => undefined), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var j = fun((Math.random() < 0.5 ? (x => { x(undefined); return x; }) : (x => undefined)), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var k = fun((Math.random() < 0.5 ? (x => { x(undefined); return x; }) : (x => undefined)), x => { x(undefined); return x; }, 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + var l = fun(((Math.random() < 0.5 ? ((x => { x(undefined); return x; })) : ((x => undefined)))),((x => { x(undefined); return x; })), 10); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + + var lambda1: (x: number) => number = x => { x(undefined); return x; }; + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. + var lambda2: (x: number) => number = (x => { x(undefined); return x; }); + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2347: Untyped function calls may not accept type arguments. + + type ObjType = { x: (p: number) => string; y: (p: string) => number }; + var obj1: ObjType = { x: x => (x, undefined), y: y => (y, undefined) }; + var obj2: ObjType = ({ x: x => (x, undefined), y: y => (y, undefined) }); \ No newline at end of file diff --git a/tests/baselines/reference/parenthesizedContexualTyping2.js b/tests/baselines/reference/parenthesizedContexualTyping2.js new file mode 100644 index 00000000000..724cbaba2b6 --- /dev/null +++ b/tests/baselines/reference/parenthesizedContexualTyping2.js @@ -0,0 +1,126 @@ +//// [parenthesizedContexualTyping2.ts] +// These tests ensure that in cases where it may *appear* that a value has a type, +// they actually are properly being contextually typed. The way we test this is +// that we invoke contextually typed arguments with type arguments. +// Since 'any' cannot be invoked with type arguments, we should get errors back. + +type FuncType = (x: (p: T) => T) => typeof x; + +function fun(f: FuncType, x: T): T; +function fun(f: FuncType, g: FuncType, x: T): T; +function fun(...rest: any[]): T { + return undefined; +} + +var a = fun(x => { x(undefined); return x; }, 10); +var b = fun((x => { x(undefined); return x; }), 10); +var c = fun(((x => { x(undefined); return x; })), 10); +var d = fun((((x => { x(undefined); return x; }))), 10); + +var e = fun(x => { x(undefined); return x; }, x => { x(undefined); return x; }, 10); +var f = fun((x => { x(undefined); return x; }),(x => { x(undefined); return x; }), 10); +var g = fun(((x => { x(undefined); return x; })),((x => { x(undefined); return x; })), 10); +var h = fun((((x => { x(undefined); return x; }))),((x => { x(undefined); return x; })), 10); + +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? x => { x(undefined); return x; } : x => undefined), 10); +var j = fun((Math.random() < 0.5 ? (x => { x(undefined); return x; }) : (x => undefined)), 10); +var k = fun((Math.random() < 0.5 ? (x => { x(undefined); return x; }) : (x => undefined)), x => { x(undefined); return x; }, 10); +var l = fun(((Math.random() < 0.5 ? ((x => { x(undefined); return x; })) : ((x => undefined)))),((x => { x(undefined); return x; })), 10); + +var lambda1: (x: number) => number = x => { x(undefined); return x; }; +var lambda2: (x: number) => number = (x => { x(undefined); return x; }); + +type ObjType = { x: (p: number) => string; y: (p: string) => number }; +var obj1: ObjType = { x: x => (x, undefined), y: y => (y, undefined) }; +var obj2: ObjType = ({ x: x => (x, undefined), y: y => (y, undefined) }); + +//// [parenthesizedContexualTyping2.js] +// These tests ensure that in cases where it may *appear* that a value has a type, +// they actually are properly being contextually typed. The way we test this is +// that we invoke contextually typed arguments with type arguments. +// Since 'any' cannot be invoked with type arguments, we should get errors back. +function fun() { + var rest = []; + for (var _i = 0; _i < arguments.length; _i++) { + rest[_i - 0] = arguments[_i]; + } + return undefined; +} +var a = fun(function (x) { + x(undefined); + return x; +}, 10); +var b = fun((function (x) { + x(undefined); + return x; +}), 10); +var c = fun(((function (x) { + x(undefined); + return x; +})), 10); +var d = fun((((function (x) { + x(undefined); + return x; +}))), 10); +var e = fun(function (x) { + x(undefined); + return x; +}, function (x) { + x(undefined); + return x; +}, 10); +var f = fun((function (x) { + x(undefined); + return x; +}), (function (x) { + x(undefined); + return x; +}), 10); +var g = fun(((function (x) { + x(undefined); + return x; +})), ((function (x) { + x(undefined); + return x; +})), 10); +var h = fun((((function (x) { + x(undefined); + return x; +}))), ((function (x) { + x(undefined); + return x; +})), 10); +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? function (x) { + x(undefined); + return x; +} : function (x) { return undefined; }), 10); +var j = fun((Math.random() < 0.5 ? (function (x) { + x(undefined); + return x; +}) : (function (x) { return undefined; })), 10); +var k = fun((Math.random() < 0.5 ? (function (x) { + x(undefined); + return x; +}) : (function (x) { return undefined; })), function (x) { + x(undefined); + return x; +}, 10); +var l = fun(((Math.random() < 0.5 ? ((function (x) { + x(undefined); + return x; +})) : ((function (x) { return undefined; })))), ((function (x) { + x(undefined); + return x; +})), 10); +var lambda1 = function (x) { + x(undefined); + return x; +}; +var lambda2 = (function (x) { + x(undefined); + return x; +}); +var obj1 = { x: function (x) { return (x, undefined); }, y: function (y) { return (y, undefined); } }; +var obj2 = ({ x: function (x) { return (x, undefined); }, y: function (y) { return (y, undefined); } }); diff --git a/tests/baselines/reference/parenthesizedContexualTyping3.js b/tests/baselines/reference/parenthesizedContexualTyping3.js new file mode 100644 index 00000000000..69784d14ce4 --- /dev/null +++ b/tests/baselines/reference/parenthesizedContexualTyping3.js @@ -0,0 +1,35 @@ +//// [parenthesizedContexualTyping3.ts] + +// Contextual typing for parenthesized substitution expressions in tagged templates. + +/** + * tempFun - Can't have fun for too long. + */ +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T { + return g(x); +} + +var a = tempFun `${ x => x } ${ 10 }` +var b = tempFun `${ (x => x) } ${ 10 }` +var c = tempFun `${ ((x => x)) } ${ 10 }` +var d = tempFun `${ x => x } ${ x => x } ${ 10 }` +var e = tempFun `${ x => x } ${ (x => x) } ${ 10 }` +var f = tempFun `${ x => x } ${ ((x => x)) } ${ 10 }` +var g = tempFun `${ (x => x) } ${ (((x => x))) } ${ 10 }` +var h = tempFun `${ (x => x) } ${ (((x => x))) } ${ undefined }` + +//// [parenthesizedContexualTyping3.js] +// Contextual typing for parenthesized substitution expressions in tagged templates. +function tempFun(tempStrs, g, x) { + return g(x); +} +var a = tempFun `${function (x) { return x; }} ${10}`; +var b = tempFun `${(function (x) { return x; })} ${10}`; +var c = tempFun `${((function (x) { return x; }))} ${10}`; +var d = tempFun `${function (x) { return x; }} ${function (x) { return x; }} ${10}`; +var e = tempFun `${function (x) { return x; }} ${(function (x) { return x; })} ${10}`; +var f = tempFun `${function (x) { return x; }} ${((function (x) { return x; }))} ${10}`; +var g = tempFun `${(function (x) { return x; })} ${(((function (x) { return x; })))} ${10}`; +var h = tempFun `${(function (x) { return x; })} ${(((function (x) { return x; })))} ${undefined}`; diff --git a/tests/baselines/reference/parenthesizedContexualTyping3.types b/tests/baselines/reference/parenthesizedContexualTyping3.types new file mode 100644 index 00000000000..6f9f24ef90b --- /dev/null +++ b/tests/baselines/reference/parenthesizedContexualTyping3.types @@ -0,0 +1,142 @@ +=== tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping3.ts === + +// Contextual typing for parenthesized substitution expressions in tagged templates. + +/** + * tempFun - Can't have fun for too long. + */ +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>T : T +>tempStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>g : (x: T) => T +>x : T +>T : T +>T : T +>x : T +>T : T +>T : T + +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>T : T +>tempStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>g : (x: T) => T +>x : T +>T : T +>T : T +>h : (y: T) => T +>y : T +>T : T +>T : T +>x : T +>T : T +>T : T + +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T { +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>T : T +>tempStrs : TemplateStringsArray +>TemplateStringsArray : TemplateStringsArray +>g : (x: T) => T +>x : T +>T : T +>T : T +>x : T +>T : T +>T : T + + return g(x); +>g(x) : T +>g : (x: T) => T +>x : T +} + +var a = tempFun `${ x => x } ${ 10 }` +>a : number +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>x => x : (x: number) => number +>x : number +>x : number + +var b = tempFun `${ (x => x) } ${ 10 }` +>b : any +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var c = tempFun `${ ((x => x)) } ${ 10 }` +>c : any +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var d = tempFun `${ x => x } ${ x => x } ${ 10 }` +>d : number +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>x => x : (x: number) => number +>x : number +>x : number +>x => x : (x: number) => number +>x : number +>x : number + +var e = tempFun `${ x => x } ${ (x => x) } ${ 10 }` +>e : any +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>x => x : (x: any) => any +>x : any +>x : any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var f = tempFun `${ x => x } ${ ((x => x)) } ${ 10 }` +>f : any +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>x => x : (x: any) => any +>x : any +>x : any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var g = tempFun `${ (x => x) } ${ (((x => x))) } ${ 10 }` +>g : any +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>(((x => x))) : (x: any) => any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any + +var h = tempFun `${ (x => x) } ${ (((x => x))) } ${ undefined }` +>h : any +>tempFun : { (tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; (tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; } +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>(((x => x))) : (x: any) => any +>((x => x)) : (x: any) => any +>(x => x) : (x: any) => any +>x => x : (x: any) => any +>x : any +>x : any +>undefined : undefined + diff --git a/tests/baselines/reference/taggedTemplateContextualTyping1.js b/tests/baselines/reference/taggedTemplateContextualTyping1.js index d631a718bf3..c1ae5c384e4 100644 --- a/tests/baselines/reference/taggedTemplateContextualTyping1.js +++ b/tests/baselines/reference/taggedTemplateContextualTyping1.js @@ -12,6 +12,7 @@ function tempTag1(...rest: any[]): T { // Otherwise, the arrow functions' parameters will be typed as 'any', // and it is an error to invoke an any-typed value with type arguments, // so this test will error. +tempTag1 `${ x => { x(undefined); return x; } }${ 10 }`; tempTag1 `${ x => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ 10 }`; tempTag1 `${ x => { x(undefined); return x; } }${ (y: (p: T) => T) => { y(undefined); return y } }${ undefined }`; tempTag1 `${ (x: (p: T) => T) => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ undefined }`; @@ -25,6 +26,10 @@ function tempTag1(...rest) { // Otherwise, the arrow functions' parameters will be typed as 'any', // and it is an error to invoke an any-typed value with type arguments, // so this test will error. +tempTag1 `${function (x) { + x(undefined); + return x; +}}${10}`; tempTag1 `${function (x) { x(undefined); return x; diff --git a/tests/baselines/reference/taggedTemplateContextualTyping1.types b/tests/baselines/reference/taggedTemplateContextualTyping1.types index a87d5eaa7a4..a6432d34f81 100644 --- a/tests/baselines/reference/taggedTemplateContextualTyping1.types +++ b/tests/baselines/reference/taggedTemplateContextualTyping1.types @@ -47,6 +47,15 @@ function tempTag1(...rest: any[]): T { // Otherwise, the arrow functions' parameters will be typed as 'any', // and it is an error to invoke an any-typed value with type arguments, // so this test will error. +tempTag1 `${ x => { x(undefined); return x; } }${ 10 }`; +>tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } +>x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T +>x : (p: T) => T +>x(undefined) : number +>x : (p: T) => T +>undefined : undefined +>x : (p: T) => T + tempTag1 `${ x => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ 10 }`; >tempTag1 : { (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, x: T): T; (templateStrs: TemplateStringsArray, f: (x: (p: T) => T) => (p: T) => T, h: (x: (p: T) => T) => (p: T) => T, x: T): T; } >x => { x(undefined); return x; } : (x: (p: T) => T) => (p: T) => T diff --git a/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping1.ts b/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping1.ts new file mode 100644 index 00000000000..49dfe75ccc6 --- /dev/null +++ b/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping1.ts @@ -0,0 +1,29 @@ + +function fun(g: (x: T) => T, x: T): T; +function fun(g: (x: T) => T, h: (y: T) => T, x: T): T; +function fun(g: (x: T) => T, x: T): T { + return g(x); +} + +var a = fun(x => x, 10); +var b = fun((x => x), 10); +var c = fun(((x => x)), 10); +var d = fun((((x => x))), 10); + +var e = fun(x => x, x => x, 10); +var f = fun((x => x), (x => x), 10); +var g = fun(((x => x)), ((x => x)), 10); +var h = fun((((x => x))), ((x => x)), 10); + +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? x => x : x => undefined), 10); +var j = fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), 10); +var k = fun((Math.random() < 0.5 ? (x => x) : (x => undefined)), x => x, 10); +var l = fun(((Math.random() < 0.5 ? ((x => x)) : ((x => undefined)))), ((x => x)), 10); + +var lambda1: (x: number) => number = x => x; +var lambda2: (x: number) => number = (x => x); + +type ObjType = { x: (p: number) => string; y: (p: string) => number }; +var obj1: ObjType = { x: x => (x, undefined), y: y => (y, undefined) }; +var obj2: ObjType = ({ x: x => (x, undefined), y: y => (y, undefined) }); \ No newline at end of file diff --git a/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts b/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts new file mode 100644 index 00000000000..5d235bff348 --- /dev/null +++ b/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts @@ -0,0 +1,35 @@ +// These tests ensure that in cases where it may *appear* that a value has a type, +// they actually are properly being contextually typed. The way we test this is +// that we invoke contextually typed arguments with type arguments. +// Since 'any' cannot be invoked with type arguments, we should get errors back. + +type FuncType = (x: (p: T) => T) => typeof x; + +function fun(f: FuncType, x: T): T; +function fun(f: FuncType, g: FuncType, x: T): T; +function fun(...rest: any[]): T { + return undefined; +} + +var a = fun(x => { x(undefined); return x; }, 10); +var b = fun((x => { x(undefined); return x; }), 10); +var c = fun(((x => { x(undefined); return x; })), 10); +var d = fun((((x => { x(undefined); return x; }))), 10); + +var e = fun(x => { x(undefined); return x; }, x => { x(undefined); return x; }, 10); +var f = fun((x => { x(undefined); return x; }),(x => { x(undefined); return x; }), 10); +var g = fun(((x => { x(undefined); return x; })),((x => { x(undefined); return x; })), 10); +var h = fun((((x => { x(undefined); return x; }))),((x => { x(undefined); return x; })), 10); + +// Ternaries in parens +var i = fun((Math.random() < 0.5 ? x => { x(undefined); return x; } : x => undefined), 10); +var j = fun((Math.random() < 0.5 ? (x => { x(undefined); return x; }) : (x => undefined)), 10); +var k = fun((Math.random() < 0.5 ? (x => { x(undefined); return x; }) : (x => undefined)), x => { x(undefined); return x; }, 10); +var l = fun(((Math.random() < 0.5 ? ((x => { x(undefined); return x; })) : ((x => undefined)))),((x => { x(undefined); return x; })), 10); + +var lambda1: (x: number) => number = x => { x(undefined); return x; }; +var lambda2: (x: number) => number = (x => { x(undefined); return x; }); + +type ObjType = { x: (p: number) => string; y: (p: string) => number }; +var obj1: ObjType = { x: x => (x, undefined), y: y => (y, undefined) }; +var obj2: ObjType = ({ x: x => (x, undefined), y: y => (y, undefined) }); \ No newline at end of file diff --git a/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping3.ts b/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping3.ts new file mode 100644 index 00000000000..004617c3d81 --- /dev/null +++ b/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping3.ts @@ -0,0 +1,21 @@ +// @target: ES6 + +// Contextual typing for parenthesized substitution expressions in tagged templates. + +/** + * tempFun - Can't have fun for too long. + */ +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T; +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, h: (y: T) => T, x: T): T; +function tempFun(tempStrs: TemplateStringsArray, g: (x: T) => T, x: T): T { + return g(x); +} + +var a = tempFun `${ x => x } ${ 10 }` +var b = tempFun `${ (x => x) } ${ 10 }` +var c = tempFun `${ ((x => x)) } ${ 10 }` +var d = tempFun `${ x => x } ${ x => x } ${ 10 }` +var e = tempFun `${ x => x } ${ (x => x) } ${ 10 }` +var f = tempFun `${ x => x } ${ ((x => x)) } ${ 10 }` +var g = tempFun `${ (x => x) } ${ (((x => x))) } ${ 10 }` +var h = tempFun `${ (x => x) } ${ (((x => x))) } ${ undefined }` \ No newline at end of file diff --git a/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts b/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts index c15d911ee52..84562f788f3 100644 --- a/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts +++ b/tests/cases/conformance/expressions/contextualTyping/taggedTemplateContextualTyping1.ts @@ -12,6 +12,7 @@ function tempTag1(...rest: any[]): T { // Otherwise, the arrow functions' parameters will be typed as 'any', // and it is an error to invoke an any-typed value with type arguments, // so this test will error. +tempTag1 `${ x => { x(undefined); return x; } }${ 10 }`; tempTag1 `${ x => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ 10 }`; tempTag1 `${ x => { x(undefined); return x; } }${ (y: (p: T) => T) => { y(undefined); return y } }${ undefined }`; tempTag1 `${ (x: (p: T) => T) => { x(undefined); return x; } }${ y => { y(undefined); return y; } }${ undefined }`;