Skip to content

Instantly share code, notes, and snippets.

@resynth1943
Created July 19, 2019 10:37
Show Gist options
  • Save resynth1943/65f5eb3e13233b2a3b62e6cd59f98e87 to your computer and use it in GitHub Desktop.
Save resynth1943/65f5eb3e13233b2a3b62e6cd59f98e87 to your computer and use it in GitHub Desktop.
TypeScript - Opaque Types
type Opaque<V> = V & { readonly __opq__: unique symbol };
type AccountNumber = Opaque<number>;
type AccountBalance = Opaque<number>;
function createAccountNumber (): AccountNumber {
return 2 as AccountNumber;
}
function getMoneyForAccount (accountNumber: AccountNumber): AccountBalance {
return 4 as AccountBalance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment