我知道assertion functions是什么,也知道其他内联type assertions表单.我有两个问题.1.为什么它们的工作方式不同(为类型脚本提供相同级别的断言)和2.有可能获得与内联断言函数相同的效果(即,是否有其他形式的内联断言我缺失)?
示例:我正在对typia进行运行时验证,如下所示:
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
}
上述方法运行良好.TypeScrip接受v是assertPoiint断言函数之后的点.但如果我使用这些type assertions的形式,它是doesn't:
...
v = <Point> typia.assertEquals<Point>(v);
v = typia.assertEquals<Point>(v) as Point;
为什么会这样呢?它们不都只是输入断言吗?有没有一种内联类型断言形式,它的工作原理与断言函数类似,可以用来避免在运行时为想要断言的每种类型编写函数?