In TypeScript 3.7, a new feature allows the writing of "assertion functions." One example is shown below:
export type TOfferAttrName = keyof typeof offerAttrMap;
export const assertIsOfferAttrName = (name: string): asserts name is TOfferAttrName => {
if (!Object.prototype.hasOwnProperty.call(offerAttrMap, name)) {
throw new Error('It is required that property name is an allowed one');
}
};
An issue arises where there is no enforcement for writing a correct assertion function. It's possible to leave the function body empty and TypeScript will not raise any errors, making it similar to optimistic typecasting with as
.
Is there a way in TypeScript to validate and ensure the correctness of an assertion function?