Skip to content

Instantly share code, notes, and snippets.

@mserranom
Created August 27, 2018 11:13
Show Gist options
  • Select an option

  • Save mserranom/0ec9ac4c7cc5023ebb57291e22bb69de to your computer and use it in GitHub Desktop.

Select an option

Save mserranom/0ec9ac4c7cc5023ebb57291e22bb69de to your computer and use it in GitHub Desktop.
// THIS IS NOT WORKING!
type A = {
type: "A";
aprop: number;
};
type B = {
type: "B";
otherprops: number;
};
type U = A | B;
let zz: any = 1;
let x: U = zz;
if (x.type === "A") {
x.aprop;
}
if (x.type === "B") {
x.otherprops;
}
const afunc = () => ({ type: "A", aprop: 2 });
const bfunc = () => ({ type: "B", otherprops: 2 });
// function afunc(): A {
// //if I remove A it won't work!
// return { type: "A", aprop: 2 };
// }
// function bfunc(): B {
// return { type: "B", otherprops: 2 };
// }
type U2 = ReturnType<typeof afunc> | ReturnType<typeof bfunc>;
let x2: U2 = zz;
if (x2.type === "A") {
x2.aprop;
}
if (x2.type === "B") {
x2.otherprops;
}
let a3: ReturnType<typeof afunc> = zz;
let b3: ReturnType<typeof bfunc> = zz;
a3.aprop;
b3.otherprops;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment