Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. export opaque type ID: string = string;
  2.  
  3.  
  4. function validateID(x: string): ID | void {
  5. if ( /* some validity check passes */ ) {
  6. return x;
  7. }
  8.  
  9. return undefined;
  10. }
  11.  
  12. import type {ID} from './exports';
  13.  
  14. function formatID(x: ID): string {
  15. return "ID: " + x; // Ok! IDs are strings.
  16. }
  17.  
  18. function toID(x: string): ID {
  19. return x; // Error: strings are not IDs.
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement