Closed
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!