@typescript-package/descriptor
Version:
A lightweight TypeScript library for property descriptor.
121 lines (120 loc) • 6.44 kB
TypeScript
import { CommonDescriptor } from './common-descriptor.abstract';
import { DataPropertyDescriptor } from '@typedly/descriptor';
import { AnyPropertyDescriptor } from '@typedly/descriptor';
import { ObjectPropertyDescriptors } from '@typedly/descriptor';
import { ValidationCallback } from '@typedly/callback';
import { ThisAccessorPropertyDescriptor } from '@typedly/descriptor';
/**
* @description
* @export
* @class Descriptor
* @template {object} [Obj=object]
* @template {keyof Obj} [PropertyName=keyof Obj]
* @template [Value=Obj[PropertyName]]
*/
export declare class Descriptor<Obj extends object = object, PropertyName extends keyof Obj = keyof Obj, Value = Obj[PropertyName]> extends CommonDescriptor {
/**
* @description Returns accessor descriptor of a `ThisAccessorPropertyDescriptor<Value, Obj>` type, on `get` or `set` property detected.
* @param descriptor An `object` of a `ThisAccessorPropertyDescriptor<Value, Obj>` type, to define with the default values of the
* `CommonDescriptor`.
* @param onValidate An optional `ValidationCallback` function to handle the result of the check whether or not the `descriptor` is an
* `object` with `get` or `set` property, by default it uses `accessorCallback()` function from the `guard`.
* @throws Throws an `Error` if the `descriptor` is not an `object` of a `ThisAccessorPropertyDescriptor<Value, Obj>` type,
* which means it doesn't contain `get` or `set` property.
* @returns The return value is an `object` of a `ThisAccessorPropertyDescriptor<Value, Obj>` type.
*/
static defineAccessor<Value, Obj extends object>(descriptor: ThisAccessorPropertyDescriptor<Value, Obj>, onValidate?: ValidationCallback): ThisAccessorPropertyDescriptor<Value, Obj> | undefined;
/**
* @description Returns data descriptor of a `DataPropertyDescriptor<Value>` interface, on `writable` or `value` property detected.
* @param descriptor An `object` of a `DataPropertyDescriptor<Value>` interface, to set with the default values of the
* `CommonDescriptor`.
* @param onValidate An optional `ValidationCallback` function to handle the result of the check whether or not the `descriptor` is an `object`
* with the `writable` or `value` property, by default it uses `dataCallback()` function from the static `DataPropertyDescriptors.guard()` method.
* @returns The return value is an `object` of a `DataPropertyDescriptor<Value>` interface.
*/
static defineData<Value>(descriptor: DataPropertyDescriptor<Value>, onValidate?: ValidationCallback): DataPropertyDescriptor<Value> | undefined;
/**
* @description Returns property descriptors from the specified object and its prototype.
* @param object An `object` of a generic `Obj` type to get property descriptors.
* @returns The return value is an `object` of a `ObjectPropertyDescriptors<Obj>` type.
*/
static fromObject<Obj extends object>(object: Obj): ObjectPropertyDescriptors<Obj> | undefined;
/**
* Returns property descriptor from the `object` or `class` prototype.
* Wrapper function for the `getOwnPropertyDescriptor`, which "Gets the own property descriptor of the specified object."
* @param object An `object` of a generic `Obj` type or a class to get own property descriptor with the specified `key`.
* If `class` is provided then it uses its prototype to get the property descriptor.
* @param key A `keyof Obj` value to get property descriptor from the `object`.
* @returns The return value is an `object` of a `PropertyDescriptor` interface or an `undefined`.
*/
static fromProperty<Obj extends object, Key extends keyof Obj>(object: Obj, key: Key): PropertyDescriptor | undefined;
/**
* @alias fromProperty()
*/
static get<Obj extends object, Name extends keyof Obj>(object: Obj, name: Name): PropertyDescriptor | undefined;
/**
* @alias fromObject()
*/
static getAll<Obj extends object>(object: Obj): ObjectPropertyDescriptors<Obj> | undefined;
/**
*
* @param object
* @param names
* @returns
*/
static pick<Obj extends object | Function, Names extends keyof Obj>(object: Obj, ...names: Names[]): Pick<ObjectPropertyDescriptors<Obj>, Names>;
/**
* The static getter accessor to define `accessor` and `data` descriptor.
* @returns The returned value is an `object` with `accessor` and `data` properties.
*/
static get define(): {
accessor: <Value, Obj extends object>(descriptor: ThisAccessorPropertyDescriptor<Value, Obj>, callback?: ValidationCallback) => ThisAccessorPropertyDescriptor<Value, Obj> | undefined;
data: <Value>(descriptor: DataPropertyDescriptor<Value>, callback?: ValidationCallback) => DataPropertyDescriptor<Value> | undefined;
};
/**
* The static getter accessor to get descriptors from property or object.
* @returns The returned value is an `object` with `object` and `property` properties.
*/
static get from(): {
object: <Obj extends object>(object: Obj) => ObjectPropertyDescriptors<Obj> | undefined;
property: <Obj extends object, Name extends keyof Obj>(object: Obj, name: Name) => PropertyDescriptor | undefined;
};
/**
* @description
* @public
* @type {?() => Value}
*/
get?: () => Value;
/**
* @description
* @public
* @type {?(value: Value) => void}
*/
set?: (value: Value) => void;
/**
* @description
* @public
* @type {?Value}
*/
value?: Value;
/**
* @description
* @public
* @type {?boolean}
*/
writable?: boolean;
/**
* Creates an instance of `Descriptor`.
* @constructor
* @param {AnyPropertyDescriptor<Value, Obj>} [param0={}]
* @param {AnyPropertyDescriptor<Value, Obj>} param0.configurable
* @param {AnyPropertyDescriptor<Value, Obj>} param0.enumerable
* @param {AnyPropertyDescriptor<Value, Obj>} param0.get
* @param {AnyPropertyDescriptor<Value, Obj>} param0.set
* @param {AnyPropertyDescriptor<Value, Obj>} param0.value
* @param {AnyPropertyDescriptor<Value, Obj>} param0.writable
* @param {?Obj} [object]
* @param {?PropertyName} [key]
*/
constructor({ configurable, enumerable, get, set, value, writable }?: AnyPropertyDescriptor<Value, Obj>, object?: Obj, key?: PropertyName);
}