Skip to content

Contextually type the rightmost operand of comma operators and logical 'AND's #6199

Closed

Description

Activity

  1. weswigham commented on Dec 22, 2015

    @weswigham
    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".

  2. DanielRosenwasser commented on Dec 22, 2015

    @DanielRosenwasser
    MemberAuthor

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

  3. weswigham commented on Dec 23, 2015

    @weswigham
    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.

  4. weswigham commented on Jan 3, 2016

    @weswigham
    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.

  5. DanielRosenwasser commented on Jan 3, 2016

    @DanielRosenwasser
    MemberAuthor

    Wesley Wigham (@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").

  6. weswigham commented on Jan 3, 2016

    @weswigham
    Member

    Aye. Parenthesis.

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

    Wesley Wigham (@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)
    .

  7. DanielRosenwasser commented on Jan 5, 2016

    @DanielRosenwasser
    MemberAuthor

    I put out a fix for this with #6355.

  8. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions