generated from garronej/ts-ci
-
Notifications
You must be signed in to change notification settings - Fork 6
/
defineAccessors.ts
31 lines (30 loc) 路 862 Bytes
/
defineAccessors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export const defineAccessors = <T extends Record<string, unknown>, 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);
},
}
: {}),
});
};