我用Typescript 写,也用type-fest
NPM包.
在我的代码中,出于文档目的,我有时喜欢添加两个类型确实相等或不相等的断言.例如,如下所示:
const b: IsEqual<{a: 1}, {a: 1}> = true;
console.log(b);
如果传递给IsEqual
的类型不相等,则在TS编译器进行类型判断期间,上述代码的第一行将失败.第二行只是为了消除TS错误variable b is defined but not used
.
到目前一切尚好.
现在,我想创建一个小实用程序,使我能够更简洁地断言两种类型的等价性,如下所示:
function type_fest_is_equal_assertion<T, S>(): void {
let R: IsEqual<T, S> = true; // *
console.log(R);
}
// with the idea that I can then simply write:
type_fest_is_equal_assertion<{a: number}, {a: number}>()
遗憾的是,在编译过程中,上面的代码在标有星号(*
)的行上失败:
类型‘Boolean’不能赋值给类型‘IsEqual<;T,S&>’.
操场是here号.
有没有办法实现我想要的东西?
顺便说一句,我也在要求澄清上述this related question中的错误消息.