Skip to content

ternary type inference is wrong #16870

Closed
Closed
@Spongman

Description

@Spongman

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

class Base { }
class Derived1 extends Base { }
class Derived2 extends Base { }

var t1 = (Math.random() < 0.5) ? "asdasd" : 34;
// t1: string | number

var t2 = (Math.random() < 0.5) ? new Derived1() : new Derived2();
// t2: Derived1

Expected behavior:

// t2: Derived1 | Derived2

I understand the shapes are the same, but this means the runtime types don't match up with the static type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions