Created
August 27, 2018 11:13
-
-
Save mserranom/0ec9ac4c7cc5023ebb57291e22bb69de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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