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

Recent --strictNullChecks issue w/ && || type result check #9113

Description

@GoToLoop

About 1 weak ago, this sample below would transpile flawlessly under --strictNullChecks:

let sinOrCos = Math.random() < .5;

let choice = sinOrCos && Math.sin || Math.cos;
//let choice = sinOrCos? Math.sin : Math.cos;

alert(choice(Math.PI));

But to my surprise today, under typescript@1.9.0-dev.20160612-1.0, only the statement using the ternary conditional operator works now!

Somehow TS thinks that choice could be boolean as well when using && + || expression. =(
Is that intentional from now on? I miss when it was smarter just 1 week ago. :-(

tsc --pretty --newLine LF --removeComments --experimentalDecorators --strictNullChecks -d -t ES6 sinCos

Activity

  1. GoToLoop commented on Jun 13, 2016

    @GoToLoop
    Author

    1 more example:

    function sq(n?: number): number {
      const r = n !== undefined && n*n || 0;
      return r;
    }
    
    alert(sq(3));

    sq.ts(3,9): error TS2322: Type 'number | boolean' is not assignable to type 'number'.
    Type 'boolean' is not assignable to type 'number'.

  2. kitsonk commented on Jun 13, 2016

    @kitsonk
    Contributor

    Like due to changes in #8949 which resolved #8892.

  3. ahejlsberg commented on Jun 13, 2016

    @ahejlsberg
    Member

    I see what's going on here. The issue is that we give the sub-expression sinOrCos && Math.sin the type ((x: number) => number) | boolean. Since the result could be the boolean value false, this is technically not wrong. But it is a little bit too permissive. The type really should be ((x: number) => number) | false, meaning that the only the boolean value false is possible. We should likewise return T | 0 and T | "" for && operations where the first operand is of type number or string respectively.

    Then we should furthermore say that in an x || y operation, we remove the definitely-falsy types (undefined, null, void, false, 0, and "") from the type of x in the resulting union type.

    Now, the only problem with this solution is that we don't (yet) support boolean literal types or numeric literal types. This means we need to do one of the following:

    Adding Mohamed Hegazy (@mhegazy) and Ryan Cavanaugh (@RyanCavanaugh).

  4. zpdDG4gta8XKpMCd commented on Jun 13, 2016

    @zpdDG4gta8XKpMCd

    Add support for boolean and numeric literal types now.

    👍

  5. zpdDG4gta8XKpMCd commented on Jun 13, 2016

    @zpdDG4gta8XKpMCd

    and numeric ranges:

    type Positive = number > 0;
    
  6. GoToLoop commented on Jun 13, 2016

    @GoToLoop
    Author

    Numeric ranges? Are those even possible? That'd be über cool though.
    I always wanted to use boolean literals for overloaded functions just like strings do.
    I wonder whether those proposed numeric literals would include enums as well?
    But of course the most important thing now is to have the && + || archaic idiom back under --strictNullChecks! :P

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

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions