Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charlypoly/b8fed7a2cdb1c64a8071e586748b5a7a to your computer and use it in GitHub Desktop.
Save charlypoly/b8fed7a2cdb1c64a8071e586748b5a7a to your computer and use it in GitHub Desktop.
TypeScript lodash `isString()` typeguard usage
import { isString } from "lodash"
let name: string | undefined = "John"
// `name` is of type `string | undefined`
let price: number | undefined = 1
// `price` is of type `number | undefined`
if (isString(name)) {
name
// `name` is of type `string`
} else if (isString(price)) {
price
// `price` is of type `never`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment