Skip to content

generic type inference for boolean literals fails #19896

Closed

Description

TypeScript Version: 2.6.1
Code

function test<T>(x: T, f: (y: T) => void) { return x; }
let z: false = false;
test(z, x => { z = x; });
// ------------^
// [ts]
// Type 'boolean | undefined' is not assignable to type 'false | undefined'.
// Type 'true' is not assignable to type 'false | undefined'.

Expected behavior:

z and x should be of the same type

Actual behavior:

x is being inferred as boolean instead of false

Activity

  1. patsissons commented on Nov 10, 2017

    @patsissons
    Author

    I'll try and test this in @next when i get a chance, just trying to get this issue posted for now.

    confirmed on 2.7.0-dev.20171109:

    tsc "test.ts"
    
    test.ts(3,16): error TS2322: Type 'boolean' is not assignable to type 'false'.
    
  2. Proculopsis commented on Nov 18, 2017

    @Proculopsis

    // let z: false = false;
    let z: boolean = false;

    Isn't this a compiler bug, letting you use 'false' as a type rather than 'boolean'?

  3. patsissons commented on Nov 18, 2017

    @patsissons
    Author

    typescript permits boolean literals as type declarators. this is particularly relevant in React development, since false means do not render anything, but true cannot be rendered.

  4. RyanCavanaugh commented on Nov 20, 2017

    @RyanCavanaugh
    Member

    Mohamed Hegazy (@mhegazy) what is the inferred suggestion here?

  5. Qwertiy commented on Jul 11, 2020

    @Qwertiy

    Seems working fine on 3.9.2.

  6. jakebailey commented on Jul 9, 2023

    @jakebailey
    Member

    Just looking at old issues; this was fixed back by #27042.

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

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions