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 the rightmost operand of comma operators and logical 'AND's #6199

Closed
DanielRosenwasser opened this issue Dec 22, 2015 · 7 comments
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

See #6196 (comment)

@weswigham
Copy link
Member

&& is probably important since we use it's type in narrowing. Also i think its required to support the "short circuiting as ternary" pattern - if ("foo" === a && "foo" || "bar"). If the && doesn't get contextually typed here, I think we get string | "bar" for the rhs type, rather than "foo" | "bar".

@DanielRosenwasser
Copy link
Member Author

You'd just get string actually. Did you mean if ("foo" === a && a || "bar") with your type narrowing change applied?

@weswigham
Copy link
Member

Maybe? I'm just pretty sure the rhs operand of && should get contextually typed, mostly so you get consistent results with narrowing within/involving compound binary logic. In my example above I'm pretty sure I want the type of the RHS expression to be contextually typed as "foo"|"bar" rather than as string because this matches how a functionally equivalent ternary would have worked. I think.

@weswigham
Copy link
Member

So my point is that if in the statement if ("foo" === a ? "foo" : "bar") the RHS has type "foo" | "bar", the RHS of the statement if ("foo" === a && "foo" || "bar") should be the same. Mostly since if we plan to disallow if ("foo" === a ? "baz" : "bar") (as neither "baz" nor "bar" are assignable to type "foo"), it follows that if ("foo" === a && "baz" || "bar") should be the same type error by extension, since the expression is equivalent.

@DanielRosenwasser
Copy link
Member Author

@weswigham Just to clarify, did you mean to parenthesize the conditional? In other words, by "foo" === a ? "foo" : "bar" you meant "foo" === (a ? "foo" : "bar").

@weswigham
Copy link
Member

Aye. Parenthesis.

On Sun, Jan 3, 2016, 12:30 PM Daniel Rosenwasser notifications@github.com
wrote:

@weswigham /~https://github.com/weswigham Just to clarify, did you mean
to parenthesize the conditional? In other words, by "foo" === a ? "foo" :
"bar" you meant "foo" === (a ? "foo" : "bar").


Reply to this email directly or view it on GitHub
#6199 (comment)
.

@DanielRosenwasser
Copy link
Member Author

I put out a fix for this with #6355.

@DanielRosenwasser DanielRosenwasser added Fixed A PR has been merged for this issue Committed The team has roadmapped this issue labels Jan 5, 2016
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 1.8 milestone Jan 5, 2016
@DanielRosenwasser DanielRosenwasser removed the In Discussion Not yet reached consensus label Jan 5, 2016
@DanielRosenwasser DanielRosenwasser self-assigned this Jan 5, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants