Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contextually type parenthesized expressions #1648

Merged
merged 5 commits into from
Jan 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,8 @@ module ts {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
return isContextSensitiveFunctionLikeDeclaration(<MethodDeclaration>node);
case SyntaxKind.ParenthesizedExpression:
return isContextSensitive((<ParenthesizedExpression>node).expression);
}

return false;
Expand Down Expand Up @@ -5226,6 +5228,8 @@ module ts {
case SyntaxKind.TemplateSpan:
Debug.assert(parent.parent.kind === SyntaxKind.TemplateExpression);
return getContextualTypeForSubstitutionExpression(<TemplateExpression>parent.parent, node);
case SyntaxKind.ParenthesizedExpression:
return getContextualType(<ParenthesizedExpression>parent);
}
return undefined;
}
Expand Down
24 changes: 12 additions & 12 deletions tests/baselines/reference/castTest.types
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ var p_cast = <Point> ({
>p_cast : Point
><Point> ({ x: 0, y: 0, add: function(dx, dy) { return new Point(this.x + dx, this.y + dy); }, mult: function(p) { return p; }}) : Point
>Point : Point
>({ x: 0, y: 0, add: function(dx, dy) { return new Point(this.x + dx, this.y + dy); }, mult: function(p) { return p; }}) : { x: number; y: number; add: (dx: any, dy: any) => Point; mult: (p: any) => any; }
>{ x: 0, y: 0, add: function(dx, dy) { return new Point(this.x + dx, this.y + dy); }, mult: function(p) { return p; }} : { x: number; y: number; add: (dx: any, dy: any) => Point; mult: (p: any) => any; }
>({ x: 0, y: 0, add: function(dx, dy) { return new Point(this.x + dx, this.y + dy); }, mult: function(p) { return p; }}) : { x: number; y: number; add: (dx: number, dy: number) => Point; mult: (p: Point) => Point; }
>{ x: 0, y: 0, add: function(dx, dy) { return new Point(this.x + dx, this.y + dy); }, mult: function(p) { return p; }} : { x: number; y: number; add: (dx: number, dy: number) => Point; mult: (p: Point) => Point; }

x: 0,
>x : number
Expand All @@ -75,10 +75,10 @@ var p_cast = <Point> ({
>y : number

add: function(dx, dy) {
>add : (dx: any, dy: any) => Point
>function(dx, dy) { return new Point(this.x + dx, this.y + dy); } : (dx: any, dy: any) => Point
>dx : any
>dy : any
>add : (dx: number, dy: number) => Point
>function(dx, dy) { return new Point(this.x + dx, this.y + dy); } : (dx: number, dy: number) => Point
>dx : number
>dy : number

return new Point(this.x + dx, this.y + dy);
>new Point(this.x + dx, this.y + dy) : Point
Expand All @@ -87,19 +87,19 @@ var p_cast = <Point> ({
>this.x : any
>this : any
>x : any
>dx : any
>dx : number
>this.y + dy : any
>this.y : any
>this : any
>y : any
>dy : any
>dy : number

},
mult: function(p) { return p; }
>mult : (p: any) => any
>function(p) { return p; } : (p: any) => any
>p : any
>p : any
>mult : (p: Point) => Point
>function(p) { return p; } : (p: Point) => Point
>p : Point
>p : Point

})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(2,22): error TS2339: Property 'foo' does not exist on type 'string'.
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(3,10): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'T'.
tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts(3,32): error TS2339: Property 'foo' does not exist on type 'T'.


==== tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts (1 errors) ====
==== tests/cases/compiler/contextualTypingWithFixedTypeParameters1.ts (3 errors) ====
var f10: <T>(x: T, b: () => (a: T) => void, y: T) => T;
f10('', () => a => a.foo, ''); // a is string
~~~
!!! error TS2339: Property 'foo' does not exist on type 'string'.
var r9 = f10('', () => (a => a.foo), 1); // error
var r9 = f10('', () => (a => a.foo), 1); // error
~~~
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'T'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'T' in this message should say 'number'

~~~
!!! error TS2339: Property 'foo' does not exist on type 'T'.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ var a: (a: string) => string;

// bug 786110
var r = a || ((a) => a.toLowerCase());
>r : (a: any) => any
>a || ((a) => a.toLowerCase()) : (a: any) => any
>r : (a: string) => string
>a || ((a) => a.toLowerCase()) : (a: string) => string
>a : (a: string) => string
>((a) => a.toLowerCase()) : (a: any) => any
>(a) => a.toLowerCase() : (a: any) => any
>a : any
>a.toLowerCase() : any
>a.toLowerCase : any
>a : any
>toLowerCase : any
>((a) => a.toLowerCase()) : (a: string) => string
>(a) => a.toLowerCase() : (a: string) => string
>a : string
>a.toLowerCase() : string
>a.toLowerCase : () => string
>a : string
>toLowerCase : () => string

52 changes: 52 additions & 0 deletions tests/baselines/reference/parenthesizedContexualTyping1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//// [parenthesizedContexualTyping1.ts]

function fun<T>(g: (x: T) => T, x: T): T;
function fun<T>(g: (x: T) => T, h: (y: T) => T, x: T): T;
function fun<T>(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); } });
Loading