feat(utils/objects): add defineLazyProperty

This commit is contained in:
PalmDevs 2025-06-02 15:48:06 +07:00
parent 6d19de566f
commit 6d1ac67bff
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA

View file

@ -94,3 +94,20 @@ export function mergeDeep(target: AnyObject, source: AnyObject) {
return target
}
/**
* Define a lazy property on an object that will be loaded when accessed.
*
* @param target The target object to define the property on.
* @param property The property key to define.
* @param loader The function that will be called to load the property value when accessed.
*/
export function defineLazyProperty<T extends object, K extends keyof T>(target: T, property: K, loader: () => T[K]) {
Object.defineProperty(target, property, {
configurable: true,
get() {
delete target[property]
return (target[property] = loader())
},
})
}