Skip to content

A conditional operator literal string type problem #14204

Closed
@BirdLeeSCUT

Description

@BirdLeeSCUT

TypeScript Version: 2.1.5

There is a conditional operator literal string type problem, ts cannot resolve true ? "a" : "b" as a type, which may call some errors.

for example:

type a_or_b = "a" | "b" ;
let v1 = (true ? "a" : "b");
let v2: a_or_b;
v2 = v1; // ERROR

Expected behavior:
expect no error.

Actual behavior:
but it comes up with an error as follow:
TS2322
image

I know a solution to refrain this error is changing the line 2 to be let v1: any = (true ? "a" : "b"); or let v1: a_or_b = (true ? "a" : "b"); , but this procedure seem to should be needless.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions