The product object to assert
TypeError if any required fields are missing or have incorrect types
try {
assertCompleteProductFields(product);
// product is now typed as RequiredProductFields
} catch (error) {
console.error("Invalid product:", error.message);
}
export function assertCompleteProductFields(
product: unknown,
): asserts product is RequiredProductFields {
const missingFields = checkCompleteProductFields(product);
if (missingFields.length > 0) {
throw new TypeError(
`Product does not contain or has invalid data for the following required fields: ${missingFields.join(", ")} `,
);
}
}
Asserts that a product object contains all required fields with correct types. Throws a TypeError listing any missing or invalid fields.