x/evt@v1.10.2/tools/typeSafety/defineAccessors.ts

šŸ’§EventEmitter's typesafe replacement
GitHub Stars
382
Go to Latest
defineAccessors.ts

export const defineAccessors = <T extends { [key: string]: any; }, K extends keyof T>( obj: T, propertyName: K, propertyDescriptor: { get?: () => T[K]; set?: (value: T[K]) => void; }): void => {
const { get, set } = propertyDescriptor;
Object.defineProperty( obj, propertyName, { ...(Object.getOwnPropertyDescriptor(obj, propertyName) || { "enumerable": true, "configurable": true }), ...(get !== undefined ? { "get": function() { return get.call(this); } } : {}), ...(set !== undefined ? { "set": function(value: T[K]){ set.call(this, value); } } : {}) } );
};