kysely
Version:
Type safe SQL query builder
41 lines (40 loc) • 1.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.requireAllProps = void 0;
/**
* Helper function to check listed properties according to given type. Check if all properties has been used when object is initialised.
*
* Example use:
*
* ```ts
* type SomeType = { propA: string; propB?: number; }
*
* // propB has to be mentioned even it is optional. It still should be initialized with undefined.
* const a: SomeType = requireAllProps<SomeType>({ propA: "value A", propB: undefined });
*
* // checked type is implicit for variable.
* const b = requireAllProps<SomeType>({ propA: "value A", propB: undefined });
* ```
*
* Wrong use of this helper:
*
* 1. Omit checked type - all checked properties will be expect as of type never
*
* ```ts
* const z: SomeType = requireAllProps({ propC: "no type will work" });
* ```
*
* 2. Apply to spreaded object - there is no way how to check in compile time if spreaded object contains all properties
*
* ```ts
* const y: SomeType = { propA: "" }; // valid object according to SomeType declaration
* const x = requireAllProps<SomeType>( { ... y } );
* ```
*
* @param obj object to check if all properties has been used
* @returns untouched obj parameter is returned
*/
function requireAllProps(obj) {
return obj;
}
exports.requireAllProps = requireAllProps;
;