Created
December 3, 2021 14:17
-
-
Save pwfcurry/622f19a79ba34863347a44138d12b4e7 to your computer and use it in GitHub Desktop.
TypeScript: narrowing unknown
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
// https://stackoverflow.com/questions/70028907/narrowing-an-object-of-type-unknown | |
export const hasKey = <K extends string>(key: K, object: unknown): object is Record<K, unknown> => | |
typeof object === "object" && object !== null && key in object; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment