UNPKG

980 BTypeScriptView Raw
1/**
2Returns a boolean for whether the two given types are equal.
3
4@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
5@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
6
7Use-cases:
8- If you want to make a conditional branch based on the result of a comparison of two types.
9
10@example
11```
12import type {IsEqual} from 'type-fest';
13
14// This type returns a boolean for whether the given array includes the given item.
15// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
16type Includes<Value extends readonly any[], Item> =
17 Value extends readonly [Value[0], ...infer rest]
18 ? IsEqual<Value[0], Item> extends true
19 ? true
20 : Includes<rest, Item>
21 : false;
22```
23
24@category Type Guard
25@category Utilities
26*/
27export type IsEqual<A, B> =
28 (<G>() => G extends A ? 1 : 2) extends
29 (<G>() => G extends B ? 1 : 2)
30 ? true
31 : false;