Skip to content

Union types doesn't work as expected when I'm trying to use !! #32095

@mojtabast

Description

@mojtabast

TypeScript Version: 3.5.1

Code

interface DataType{

    display_name: string | null
}

function myName(data :DataType) : string{

    if( !!data.display_name === false ) return 'No Name'

    return data.display_name
}

Expected behavior:
Typescript shouldn't complain about return data.display_name because I covered the null before this.

Actual behavior:
It saying Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'. on return data.display_name

Playground Link:
You can see the result here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions