镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

TypeScript Compiler does not understand resulting types when using && #12174

Description

TypeScript Version: 2.0.8

Code

// A *self-contained* demonstration of the problem follows...
let s:string=("" && false);

Expected behavior:
Since ("" && false) is "" in JavaScript I should be able to assign the resulting value to a string, but TypeScript thinks that this will become a boolean.
Actual behavior:
Compiler error claiming that a boolean cannot be assigned to a string

Disclaimer: I think that the behaviour of the && operator in JavaScript is horrible, however it is what it is and TypeScript should honour it.

Activity

  1. RyanCavanaugh commented on Nov 11, 2016

    @RyanCavanaugh
    Member

    Agree that's not the desired behavior, but did you find this writing real code?

  2. ahejlsberg commented on Nov 11, 2016

    @ahejlsberg
    Member

    Your example compiles with no error using the nightly build. The issue was fixed with our improved support for literal types in #10676.

  3. Ristaaf commented on Nov 16, 2016

    @Ristaaf
    Author

    RyanCavanaough: This was found when turning on strictNullChecks on an existing codebase, not the example that I showed of course, that was just a simplification to be clear.

    We had complex logical expressions with && that relied on returning a string. They could have returned other types too, but we did not handle that, so to understand what was going on and to fix the code, we started to read up on the && operator and what it returns in different circumstances, then we tried those circumstances out in TypeScript and found the error reported here.

  4. 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

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions