Last active
February 20, 2020 16:55
-
-
Save guillaumewuip/87e044d5dfe3e6ef5e7acf4f67e2ae45 to your computer and use it in GitHub Desktop.
How to model your entities - 13
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
export type $TextMessage = { | |
text: string; | |
} | |
const { toOpaque, fromOpaque, isOpaque } = createOpaqueAPI< | |
'TextMessage', | |
$TextMessage, | |
>('TextMessage’); | |
export type TextMessage = ReturnType<typeof toOpaque>; | |
export function createText(text: string): TextMessage { | |
return toOpaque({ text }); | |
} | |
export function text(message: TextMessage): string { | |
const $message = fromOpaque(message); | |
return $message.text; | |
} | |
// We can use the isOpaque type guard without needing the `messageType` tag, | |
// free of charge! | |
export function isText(message: Message): message is TextMessage { | |
return isOpaque(message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment