Ah, the type-level equality operator as requested in microsoft/TypeScript#27024. @MattMcCutchen has come up with a solution, described in a comment on that issue involving generic conditional types which does a decent job of detecting when two types are exactly equal, as opposed to just mutually assignable. In a perfectly sound type system, "mutually assignable" and "equal" would probably be the same thing, but TypeScript isn't perfectly sound. In particular, the any
type is both assignable to and assignable from any other type, meaning that string extends any ? true : false
and any extends string ? true: false
both evaluate to true
, despite the fact that string
and any
are not the same type.
Here's an IfEquals<T, U, Y, N>
type which evaluates to Y
if T
and U
are equal, and N
otherwise.
type IfEquals<T, U, Y=unknown, N=never> =
(<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2) ? Y : N;
Let's see it work:
type EQ = IfEquals<any[], [number][], "same", "different">; // "different"
Okay, those are recognized as different types. There are probably some other edge cases where two types that you think are the same are seen as different, and vice versa:
type EQ1 = IfEquals<
{ a: string } & { b: number },
{ a: string, b: number },
"same", "different">; // "different"!
type EQ2 = IfEquals<
{ (): string, (x: string): number },
{ (x: string): number, (): string },
"same", "different">; // "different", as expected, but:
type EQ3 = IfEquals<
{ (): string } & { (x: string): number },
{ (x: string): number } & { (): string },
"same", "different">; // "same"!! but they are not the same,
// intersections of functions are order-dependent
Anyway, given this type we can make a function that generates an error unless the two types are equal in this way:
/** Trigger a compiler error when a value is _not_ an exact type. */
declare const exactType: <T, U>(
draft: T & IfEquals<T, U>,
expected: U & IfEquals<T, U>
) => IfEquals<T, U>
declare let a: any[]
declare let b: [number][]
// $ExpectError
exactType(a, b) // error
Each argument has a type T
or U
(for type inference of the generic parameter) intersected with IfEquals<T, U>
so that there will be an error unless T
and U
are equal. This gives the behavior you want, I think.
Note that the arguments of this function are not optional. I don't really know why you wanted them to be optional, but (at least with --strictNullChecks
turned on) it weakens the check to do so:
declare let c: string | undefined
declare let d: string
exactType(c, d) // no error if optional parameters!
It's up to you if that matters.