Skip to content

Instantly share code, notes, and snippets.

@mikoloism
Last active May 30, 2023 08:46
Show Gist options
  • Save mikoloism/ad6da67eaf991d90d504651c5212604a to your computer and use it in GitHub Desktop.
Save mikoloism/ad6da67eaf991d90d504651c5212604a to your computer and use it in GitHub Desktop.
Global Utilities #Assign and #Object
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