Skip to content

Instantly share code, notes, and snippets.

@janjakubnanista
Created May 22, 2020 15:04
Show Gist options
  • Save janjakubnanista/f76e596fc98229d896c11aa13b9c6ce6 to your computer and use it in GitHub Desktop.
Save janjakubnanista/f76e596fc98229d896c11aa13b9c6ce6 to your computer and use it in GitHub Desktop.
TypeScript type guards using ts-type-checked
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