Skip to content

Instantly share code, notes, and snippets.

@pwfcurry
Created December 3, 2021 14:17
Show Gist options
  • Save pwfcurry/622f19a79ba34863347a44138d12b4e7 to your computer and use it in GitHub Desktop.
Save pwfcurry/622f19a79ba34863347a44138d12b4e7 to your computer and use it in GitHub Desktop.
TypeScript: narrowing unknown
// 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