Last active
May 30, 2023 08:46
-
-
Save mikoloism/ad6da67eaf991d90d504651c5212604a to your computer and use it in GitHub Desktop.
Global Utilities #Assign and #Object
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 define<T extends {}>(source: T, ...sources: any[]): T { | |
return Object.assign(source, ...sources) as T; | |
} | |
export function assign<T extends {}>(...sources: any[]): T { | |
return Object.assign({}, ...sources) as T; | |
} | |
export function sureAssign<T>( | |
value: T | undefined, | |
message?: string, | |
): T | never { | |
if (typeof value === 'undefined') { | |
throw new ReferenceError(message); | |
} | |
return value; | |
} | |
type OmitExtended<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | |
export function omit<T, K extends keyof T = keyof T>( | |
object: T, | |
properties: K[], | |
): OmitExtended<T, K> { | |
const result = { ...object } as any; | |
for (const property of properties) { | |
delete result[property]; | |
} | |
return result; | |
} | |
export default { sureAssign, define, assign, omit }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment