Description
π Search Terms
discriminated union destructuring
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about discriminated unions (there are none)
β― Playground Link
π» Code
`
interface IsLoading {
loaded: false;
obj: object | undefined;
}
interface Loaded {
loaded: true;
obj: object;
}
type MaybeLoaded = IsLoading | Loaded;
interface IsLoading2 {
loaded2: false;
obj: object | undefined;
}
interface Loaded2 {
loaded2: true;
obj: object;
}
type MaybeLoaded2 = IsLoading2 | Loaded2;
const sample = (ml: MaybeLoaded): MaybeLoaded2 => {
const { loaded, obj } = ml;
return { loaded2: loaded, obj };
};
//You can do this but this is annoying and needs useless runtime code
const sample2 = (ml: MaybeLoaded): MaybeLoaded2 => {
const { loaded, obj } = ml;
return loaded ? { loaded2: true, obj } : { loaded2: false, obj };
};
`
π Actual behavior
There's an error mapping MaybeLoading to MaybeLoading2
π Expected behavior
Both of these discriminated unions are structurally the same and should be "mappable" without having to cast using 'as' or writing needless runtime code