Created
April 20, 2023 03:49
-
-
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
This file contains hidden or 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
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