Skip to content

cannot instantiate a tagged union whose discriminant property is union type #12052

Description

Hey typescript, I'm getting an error when instantiating a tagged union whose discriminant property is inlined into the literal, vs. set via assignment right afterwards. Is there something I'm missing here?

TypeScript Version: 2.0.3 (or whatever's on http://www.typescriptlang.org/play/)

Code

interfaceILinearAxis{type: "linear";}interfaceICategoricalAxis{type: "categorical";}typeIAxis=ILinearAxis|ICategoricalAxis;typeIAxisType="linear"|"categorical";functiongetAxisType(): IAxisType{if(1==1){return"categorical";}else{return"linear";}}constbad: IAxis={type: getAxisType()};//TS2322: Type '{ type: "linear" | "categorical"; }' is not assignable to type 'ILinearAxis | ICategoricalAxis'.// Type '{ type: "linear" | "categorical"; }' is not assignable to type 'ICategoricalAxis'.// Types of property 'type' are incompatible.// Type '"linear" | "categorical"' is not assignable to type '"categorical"'.// Type '"linear"' is not assignable to type '"categorical"'.constgood: IAxis={type: undefined};good.type=getAxisType();

Expected behavior: Both should work
Actual behavior: The "bad" axis declaration fails with the error described

Thanks!

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