Created
May 22, 2020 15:04
-
-
Save janjakubnanista/f76e596fc98229d896c11aa13b9c6ce6 to your computer and use it in GitHub Desktop.
TypeScript type guards using ts-type-checked
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
import { isA, typeCheckFor } from 'ts-type-checked'; | |
// Using the typeCheckFor type guard factory | |
const isString = typeCheckFor<string>(); | |
const isWebSocketMessage = typeCheckFor<WebSocketMessage>(); | |
// Or directly checking a value somewhere in the code | |
if (isA<string>(value)) return 'Hello String'; | |
if (isA<WebSocketMessage>(value)) return 'Hello Web Socket!'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment