@rxap/utilities
Version:
A collection of utility functions, types and interfaces.
59 lines (58 loc) • 3.29 kB
TypeScript
/**
* The `Required` function is a TypeScript decorator that enforces the requirement of a specific attribute in a class.
* It throws an error if an attempt is made to access the attribute before it has been set.
*
* @template T - The type of the `this` context within the `stackOrTarget` function.
*
* @param {((this: T) => string) | any} stackOrTarget - A function that returns a string when called with the `this` context, or any other value.
* If a function is provided, it will be called with the `this` context when an error is thrown.
* If any other value is provided, it will be used as the target object on which the property is defined.
*
* @param {string} [pk] - The name of the property to be defined on the target object. If not provided, the function returns another function that takes the target object and property name as parameters.
*
* @returns {any} - If `pk` is provided, the function does not return anything. If `pk` is not provided, the function returns another function that can be used to define a property on a target object.
*
* @throws {RxapUtilitiesError} - Throws an error with a message indicating that the attribute is required if an attempt is made to access the attribute before it has been set.
*
* @example
* // If `pk` is provided
* Required(targetObject, 'propertyName');
*
* // If `pk` is not provided
* const defineProperty = Required(stackOrTarget);
* defineProperty(targetObject, 'propertyName');
*
* @remarks
* This function uses `Object.defineProperty` to define a property with a getter and setter on the target object.
* The getter throws an error if the property is accessed before it has been set.
* The setter allows the property to be set and makes it writable, configurable, and enumerable.
*
* @deprecated removed
*/
export declare function Required<T>(stackOrTarget: ((this: T) => string) | any, pk?: string): any;
/**
* Checks if a required property of a given object is defined.
*
* This function is used to verify if a specific property of an object, which is marked as required, is defined.
* It uses the Object.getOwnPropertyDescriptor method to get the property descriptor of the specified property of the object.
* The function then checks if the descriptor has a 'value' property, which indicates that the required property is defined.
*
* @template T - An object type.
* @param {T} obj - The object to check.
* @param {keyof T} propertyKey - The key of the property to check.
* @returns {boolean} - Returns true if the required property is defined, false otherwise.
*
* @example
* // Assuming we have an object like this:
* // const obj = { name: 'John', age: undefined };
* // The function can be used like this:
* // IsRequiredPropertyDefined(obj, 'name'); // returns true
* // IsRequiredPropertyDefined(obj, 'age'); // returns false
*
* @note This function is particularly useful when working with decorators like @Required in TypeScript.
* It is used to check if a FormControl instance is already assigned to the component.
* The check is done using property descriptor because the @Required decorator is used on the control property.
*
* @deprecated removed
*/
export declare function IsRequiredPropertyDefined<T extends object>(obj: T, propertyKey: keyof T): boolean;