-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
DanielRosenwasser
merged 5 commits into
master
from
contextualTypeParenthesizedExpressions
Jan 14, 2015
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d5f0281
Added tests for contextual typing on parenthesized expressions, added…
DanielRosenwasser f5f4e28
Fixed portion of test.
DanielRosenwasser cd24699
Clarified comment in test.
DanielRosenwasser 22174a1
Contextually type parenthesized expressions.
DanielRosenwasser 6783e35
Removed probably-unnecessary statement from test.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
tests/baselines/reference/contextualTypingWithFixedTypeParameters1.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. | ||
~~~ | ||
!!! error TS2339: Property 'foo' does not exist on type 'T'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/baselines/reference/parenthesizedContexualTyping1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'