When working with TypeScript, I often utilize the type-fest
npm package in my coding.
For documentation purposes, I sometimes like to assert that two types are either equal or unequal. Here is an example:
const b: IsEqual<{a: 1}, {a: 1}> = true;
console.log(b);
If the types passed to IsEqual
are not equal, the first line of code above would fail during type-checking by the TS compiler. The second line is included just to avoid the TS error
variable b is defined but not used
.
Now, I want to create a utility function that allows me to assert the equality of two types more concisely, like this:
function type_fest_is_equal_assertion<T, S>(): void {
let R: IsEqual<T, S> = true; // *
console.log(R);
}
// With this utility function, I can simply write:
type_fest_is_equal_assertion<{a: number}, {a: number}>()
However, the compilation of the above code fails at the specified line marked with an asterisk (*
) with the error message:
Type 'boolean' is not assignable to type 'IsEqual<T, S>'.
You can find the playground link here.
Do you know of a way to achieve what I am aiming for?
Additionally, I am looking for clarification on the mentioned error message in this related question.