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

interface ILinearAxis { type: "linear"; }

interface ICategoricalAxis { type: "categorical"; }

type IAxis = ILinearAxis | ICategoricalAxis;
type IAxisType = "linear" | "categorical";

function getAxisType(): IAxisType {
    if (1 == 1) {
        return "categorical";
    } else {
        return "linear";
    }
}

const bad: 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"'.

const good: IAxis = { type: undefined };
good.type = getAxisType();

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

Thanks!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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