Skip to content

Commit

Permalink
Add test case to demonstrate why we can't use a naive implementation of
Browse files Browse the repository at this point in the history
Equals #33
  • Loading branch information
garronej committed Jul 24, 2024
1 parent 0aca8be commit ba68f11
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import type { Unite } from "./tools/Unite";
import type { StrictEquals } from "./tools/StrictEquals";

// Credit https://stackoverflow.com/a/53808212/3731798
/** https://docs.tsafe.dev/main/equals */
export type Equals<A1, A2> = StrictEquals<Unite<A1>, Unite<A2>>;

// Credit https://stackoverflow.com/a/52473108/3570903
// TODO: To export and document, this type can be used in place of equals if the types to compare have no `any` in them.
// (I think but some more testing is needed)
// export type LooseEquals<T, U> = T extends U ? (U extends T ? true : false) : false;
28 changes: 28 additions & 0 deletions test/Equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,31 @@ type Square = {

assert<Equals<A, B>>();
}

// NOTE: This example shows why we can't use `type Equals<T, U> = T extends U ? (U extends T ? true : false) : false;`
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type A = any[];
type B = [number][];

//@ts-expect-error: They are not equal
assert<Equals<A, B>>();
}

{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type A = unknown[];
type B = [unknown][];

//@ts-expect-error: They are not equal
assert<Equals<A, B>>();
}

{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type A = any;
type B = number;

//@ts-expect-error: They are not equal
assert<Equals<A, B>>();
}

0 comments on commit ba68f11

Please sign in to comment.