Last active
February 22, 2020 02:14
-
-
Save Jason5Lee/333bd4255d8985b5355ede2ad403f8c0 to your computer and use it in GitHub Desktop.
TypeScript's opaque type.
This file contains 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
type USD = number & { readonly __brand: unique symbol } | |
type EUR = number & { readonly __brand: unique symbol } | |
type Currency = USD | EUR; | |
function addCurrency<T extends Currency>(a: T, b: T): T { | |
return (a + b) as T; | |
} | |
addCurrency(5 as USD, 6 as USD); | |
addCurrency(5 as EUR, 7 as USD); // Compilation error. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment