Type equality approximations
Concept and syntax
Mutual assignability is a useful approximation of type equality, but any can make unrelated types pass. Wrapping the compared types prevents union distribution. A separate any detector can distinguish this special case; there is no general built-in semantic equality operator for all TypeScript types.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Mutual<A,B>=[A] extends [B]?([B] extends [A]?true:false):false; type IsAny<T>=0 extends (1 & T)?true:false;const a:Mutual<any,string>=true; const b:IsAny<any>=true; const c:Mutual<"x"|"y","y"|"x">=true; console.log(a,b,c);Expected output:
true true trueCommon mistake
Intentionally invalid; compile separately and do not execute.
type Mutual<A,B>=[A] extends [B]?([B] extends [A]?true:false):false; type IsAny<T>=0 extends (1 & T)?true:false;const a:Mutual<"x",string>=true;Expected diagnostic: TS2322. string cannot be assigned back to the single literal x.
Independent comparison
The any detector distinguishes unknown in this example. Expected: 'false\n'
type IsAny<T>=0 extends (1&T)?true:false; const n:IsAny<unknown>=false; console.log(n);Practice and recap
Compare Mutual<unknown,any> and IsAny<unknown>.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Type equality approximations — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Type equality approximations is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Mutual<A,B>=[A] extends [B]?([B] extends [A]?true:false):false; type IsAny<T>=0 extends (1 & T)?true:false;const a:Mutual<any,string>=true; const b:IsAny<any>=true; const c:Mutual<"x"|"y","y"|"x">=true; console.log(a,b,c);Which diagnostic and explanation match the rejected example under the lesson settings?
type Mutual<A,B>=[A] extends [B]?([B] extends [A]?true:false):false; type IsAny<T>=0 extends (1 & T)?true:false;const a:Mutual<"x",string>=true;What is IsAny<unknown>?