Skip to content

Union type allows any string value casted to an enum #29456

@bariscicek

Description

@bariscicek

TypeScript Version: 3.3.0-dev.20190117

Search Terms: enum, casting enum, union type

Code

 const enum Direction {                                                                                                                                                                  
     LEFT = "LEFT",                                                                                                                                                                      
     RIGHT = "RIGHT"                                                                                                                                                                     
 }                                                                                                                                                                                       
                                                                                                                                                                                         
 type DirectionType = "UP" | "DOWN" | Direction;                                                                                                                                         
                                                                                                                                                                                         
 const myDirection: DirectionType = <Direction>"SOUTHWEST"; // no warning or error                                                                                                         
                                                                            

Expected behavior:
Trying to assign "SOUTHWEST" to myDirection should give an error.

Actual behavior:
No warning or error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions