Having a good understanding of assertion functions and other inline forms of type assertions in TypeScript, I have two interconnected questions. Firstly, why do they not provide the same level of assertion to TypeScript? Secondly, is there a way to achieve the same effect as an assertion function inline or is there another form of inline assertion that I might be overlooking?
For instance, when conducting runtime validation using typia, my code looks like this:
import typia from "typia";
interface Point {
x: number;
y: number;
tag?: string;
}
function x(v: unknown) {
assertPoint(v);
console.log(v.x + v.y);
}
function assertPoint(v: unknown): asserts v is Point {
typia.assertEquals<Point>(v); // https://typia.io/docs/validators/assert/#assertequals-function
}
The above implementation works smoothly. TypeScript recognizes that `v` conforms to the Point interface after invoking the assertPoint assertion function. However, it fails if I utilize these alternate forms of type assertions:
...
v = <Point> typia.assertEquals<Point>(v);
v = typia.assertEquals<Point>(v) as Point;
Why does this happen? Aren't they both simply type assertions? Furthermore, is there an inline form of type assertion that behaves like an assertion function, eliminating the need for creating a function for every type I intend to assert at runtime?