import { MutuallyAssignable, ShallowResolve } from "."; /** * checks whether types `A` and `B` are considered "identical" internally by TS. * note that this is in regards to the types themselves, _not_ necessarily what they get reduced to. * * one not uncommon use case affected by this shortcoming is in it not properly handling intersections * (e.g. `{ a: 0 } & { b: 1 }` is not considered identical to `{ a: 0, b: 1 }`) * * it may also fail on certain forms of variadic tuples (e.g. `[...T[], T]` is erroneously considered identical to `T[]`) * * adapted from MattMcCutchen's answer in {@link https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650} */ type InternallyIdentical = (() => T extends (A | T) ? 0 : 1) extends (() => T extends (B | T) ? 0 : 1) ? true : false; /** * checks whether types `A` and `B` are identical. * * does not resolve intersection types (e.g. `{ a: 0 } & { b: 1 }` will be considered not equal to `{ a: 0, b: 1 }`). * * adapted from MattMcCutchen's answer in {@link https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650}. * * this is modified to try to address a niche edge case (possibly didn't even exist at the time of the answer?) with variadic tuples: * with the original code, `[...string[], string]` and `string[]` are considered "equal". * general shortcomings with alternative (i.e. different from MattMcCutchen's) `Equals` implementations regard false positives (as far as i'm aware), * not any false negatives. `MutuallyAssignable` is one such alternative implementation, that simply checks if both types can be assigned to each other. * it happens to properly handle the aforementioned variadic tuple case, and since it doesn't have any known false negatives (afaik), * it is thus used as the check for this variadic tuple case. */ type _Equals = InternallyIdentical extends true ? MutuallyAssignable : false; type Options = { resolve_intersections: RI; }; /** * whether types `A` and `B` are identical. * * by default will try to resolve intersection types (e.g. convert `{ a: 0 } & { b: 1 }` into `{ a: 0, b: 1 }`). * this is usually what you want. an option is provided to not do so as well. * * adapted from @author Matt McCutchen * @see https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650 * * @warning known issues: * - it appears certain intersections may not produce the expected result, even when `resolve_intersections` is `true`. * if you find one not already identified in tests, please submit an issue with a minimal example describing it * - function overloads can be weird and not produce the expected result. * * @since 0.0.2 * * @example * ```ts * // many of these examples taken from the thread https://github.com/Microsoft/TypeScript/issues/27024 * * type e0 = Equals // false * type e1 = Equals<1, 1> // true * type e2 = Equals // false * type e3 = Equals<1 | 2, 1> // false * type e5 = Equals // false * type e6 = Equals<[any], [number]> // false * * // object intersection * type e7 = Equals<{ x: 1 } & { y: 2 }, { x: 1, y: 2 }> // true * type e8 = Equals<{ x: 1 } & { y: 2 }, { x: 1, y: 2}, { resolve_intersections: false }> // false * * // primitive intersection * type e9 = Equals // true * type e10 = Equals // false * * // variadic tuple * * type t11a = string[] * type t11b = [string, ...string[]] * type t11c = [...string[], string] * type t11d = [string, ...string[], string] * * type e11a = Equals // false * type e11b = Equals // false * type e12a = Equals // false * type e12b = Equals // false * type e13a = Equals // false * type e13b = Equals // false * type e14a = Equals // false * type e14b = Equals // false * type e15a = Equals // false * type e15b = Equals // false * type e16a = Equals // false * type e16b = Equals // false * * // function intersection * type t17a = (x: 0, y: null) => void * type t17b = (x: number, y: string) => void * type t17c = t17a & t17b * type t17d = t17b & t17a * type e17 = Equals // false * type e18 = Equals // false * ``` */ export type Equals> = Opts extends Options ? _Equals, ShallowResolve> : _Equals; export {}; //# sourceMappingURL=equals.d.ts.map