Tone.js/Tone/core/util/Interface.ts
2019-11-03 17:42:51 -05:00

52 lines
1.6 KiB
TypeScript

import { isArray } from "./TypeCheck";
// return an interface which excludes certain keys
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
/**
* Make the property not writable using `defineProperty`. Internal use only.
*/
export function readOnly(target: object, property: string | string[]): void {
if (isArray(property)) {
property.forEach(str => readOnly(target, str));
} else {
Object.defineProperty(target, property, {
enumerable: true,
writable: false,
});
}
}
/**
* Make an attribute writeable. Internal use only.
*/
export function writable(target: object, property: string | string[]): void {
if (isArray(property)) {
property.forEach(str => writable(target, str));
} else {
Object.defineProperty(target, property, {
writable: true,
});
}
}
export const noOp: (...args: any[]) => any = () => {
// no operation here!
};
/**
* Recursive Partial taken from here: https://stackoverflow.com/a/51365037
*/
export type RecursivePartial<T> = {
[P in keyof T]?:
T[P] extends Array<infer U> ? Array<RecursivePartial<U>> :
T[P] extends object ? RecursivePartial<T[P]> :
T[P];
};
/**
* Recursive Omit modified from here: https://stackoverflow.com/a/54487392/1146428
*/
type OmitDistributive<T, K extends string | number> = T extends any ? (T extends object ? Id<RecursiveOmit<T, K>> : T) : never;
type Id<T> = {} & { [P in keyof T]: T[P]} // Cosmetic use only makes the tooltips expand the type can be removed
export type RecursiveOmit<T extends any, K extends string | number> = Omit<{ [P in keyof T]: OmitDistributive<T[P], K> }, K>;