Skip to content

Instantly share code, notes, and snippets.

@vendethiel
Forked from AlexGeb/hasDefinedKey.ts
Created March 26, 2025 16:46
Show Gist options
  • Save vendethiel/e9ea474749289da4fb4bd39be131715a to your computer and use it in GitHub Desktop.
Save vendethiel/e9ea474749289da4fb4bd39be131715a to your computer and use it in GitHub Desktop.
hasDefinedKey
export const hasDefinedKey =
<K extends PropertyKey>(key: K) =>
<T extends Partial<Record<K, unknown>>>(
obj: T,
): obj is T & Record<K, Exclude<T[K], undefined>> =>
key in obj && obj[key] !== undefined;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment