Skip to content

Instantly share code, notes, and snippets.

@bogdanovna
Created April 20, 2023 03:49
Show Gist options
  • Save bogdanovna/47cbce5a8479c2f501c81becf006702a to your computer and use it in GitHub Desktop.
Save bogdanovna/47cbce5a8479c2f501c81becf006702a to your computer and use it in GitHub Desktop.
allow you dynamicaly create array of keys (with or without some of them or new) and provide typed return value
export function computeArrOfKeys<
T extends object,
R extends readonly (keyof T)[],
A extends readonly string[]>(
obj: Required<Record<keyof Required<T>, null>>,
rm: R, ...add: A) {
const origin = new Set(Object.keys(obj));
add.forEach((a) => origin.add(a));
rm.forEach((r) => origin.delete(r as string));
return Array.from(origin) as Exclude<keyof T | A[number], R[number]>[];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment