Created
November 29, 2021 11:10
-
-
Save charlypoly/b8fed7a2cdb1c64a8071e586748b5a7a to your computer and use it in GitHub Desktop.
TypeScript lodash `isString()` typeguard usage
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 { 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