Skip to content

Instantly share code, notes, and snippets.

@attitude
Created April 3, 2025 14:23
Show Gist options
  • Save attitude/d16c08ac3b41b5cf095c93eb8d5d0c08 to your computer and use it in GitHub Desktop.
Save attitude/d16c08ac3b41b5cf095c93eb8d5d0c08 to your computer and use it in GitHub Desktop.
export type NonUndefinedKeys<T> = {
[K in keyof T]-?: T[K] extends undefined ? never : K;
}[keyof T];
export function definedProps<T extends Record<string, any>>(obj: T): NonUndefinedKeys<T> {
for (const key in obj) {
if (obj[key] === undefined) {
delete obj[key];
}
}
return obj as unknown as NonUndefinedKeys<T>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment