-
-
Notifications
You must be signed in to change notification settings - Fork 723
fix: IsEqual, {a: t, b: s} and {a: t} & {b: s} are equal
#1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taiyakihitotsu
wants to merge
36
commits into
sindresorhus:main
Choose a base branch
from
taiyakihitotsu:fix/is-equal-20260127
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
dabe872
fix: `IsEqual`, `{a: t, b: s}` and `{a: t} & {b: s}` are equal
taiyakihitotsu 74b0e71
Fix: `IsEqual`, returns `true` for nested intersection of objects
taiyakihitotsu 4c9ef58
Merge branch 'main' into fix/is-equal-20260127
taiyakihitotsu b887c8c
refactor: remoe unused imports
taiyakihitotsu df808ce
refactor: remove unused import
taiyakihitotsu b213bb7
revert: `test-d/is-equal.ts`
taiyakihitotsu 4d8773c
fix: define `UniqueUnionDeep` and `UniqueUnion`, to fix `IsEqual` ret…
taiyakihitotsu fb14a98
doc: update `UniqueUnionDeep` comment
taiyakihitotsu 7f0162f
doc: update `test-d/is-equal.ts` comment
taiyakihitotsu ba7ccab
test: add tests
taiyakihitotsu 2cc3a26
update: add `UniqueUnionDeep` to define `IsEqual` to strictly get `A|…
taiyakihitotsu 590587c
fix: rename `ExcludeExactly` from `UniqueExclude`, update for a case …
taiyakihitotsu 9af5c71
Merge branch 'main' into fix/is-equal-20260127
taiyakihitotsu 2f1d991
build: trigger CI
taiyakihitotsu d02b353
revert `IsEqual` and `Paths` changes
som-sm c6d4206
revert: re-revert d02b353, `source/is-equal.d.ts` and `test-d/is-equa…
taiyakihitotsu 687e6ed
add: `LastOfUnion`, return a type of an union-type (order is not guar…
taiyakihitotsu 631a49f
refactor: `UnionToTuple`, import `LastOfUnion` instead of define
taiyakihitotsu 755df63
Add `ExcludeExactly`, distinguish between different modifiers.
taiyakihitotsu 1f76fd5
fix: `UnionToTuple`, use `ExcludeExactly`, improve performance.
taiyakihitotsu 7d990be
Refactor: `UniqueUnion`, use `UnionToTuple`, improve performance.
taiyakihitotsu 521c373
Merge branch 'main' into fix/is-equal-20260127
taiyakihitotsu 944a74b
temp: ignore arrow-doc.
taiyakihitotsu a8c2695
refactor: `IsEqual`, remove redundant `SimplifyDeep`, fix the doc.
taiyakihitotsu 390e127
Merge branch 'main' into fix/is-equal-20260127
taiyakihitotsu 18eb4af
Revert "temp: ignore arrow-doc."
taiyakihitotsu 4282283
Update readme.md
sindresorhus c5ade48
update: `UniqueUnionDeep`, add `IsEqual`'s lambda test cases
taiyakihitotsu 5ad2c4d
update: `UniqueUnionDeep` handles records in tuples.
taiyakihitotsu 9d6aa80
test: add deep identical cases for `IsEqual` and `UniqueUnionDeep`
taiyakihitotsu 946b90f
Merge branch 'main' into fix/is-equal-20260127
taiyakihitotsu 6497043
fix: revert `IsEqual` and the tests. fix `ExcludeExactly` to improve …
taiyakihitotsu 8a7056b
refactor: rename `SimplifyUniqueUnionDeep` to `InternalUniqueUnionDeep`
taiyakihitotsu 4f490d2
fix: Overload cases
taiyakihitotsu eb8828b
fix: Branded Type with Tuple
taiyakihitotsu 0298dad
update: handles overload cases
taiyakihitotsu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import type {IsUnknown} from './is-unknown.d.ts'; | ||
| import type {IsNever} from './is-never.d.ts'; | ||
| import type {IsAny} from './is-any.d.ts'; | ||
| import type {LastOfUnion} from './last-of-union.d.ts'; | ||
|
|
||
| /** | ||
| Return `never` if the 1st and the 2nd arguments are mutually identical. | ||
| Return the 1st if not. | ||
| (But there's a limitation about union/intersection type. See `MatchOrNever` or `_IsEqual` in `source/is-equal.d.ts` doc.) | ||
|
|
||
| @example | ||
| ``` | ||
| type A = MatchOrNever<string | number, string>; // => string | number | ||
| type B = MatchOrNever<string | number, string | number>; // => never | ||
| type C = MatchOrNever<string | number, unknown>; // => string | number | ||
| type D = MatchOrNever<string, string | number>; // => string | ||
| ``` | ||
|
|
||
| This does NOT depend on assignability. | ||
|
|
||
| @example | ||
| ``` | ||
| type RO_0 = MatchOrNever<{readonly a: 0}, {a: 0}>; // => {readonly a: 0} | ||
| type RO_1 = MatchOrNever<{a: 0}, {readonly a: 0}>; // => {a: 0} | ||
| ``` | ||
|
|
||
| `unknown` and `never` cases, which easily break equality in type level code base. | ||
|
|
||
| @example | ||
| ``` | ||
| type E = MatchOrNever<unknown, never>; // => unknown | ||
| type F = MatchOrNever<unknown, unknown>; // => never | ||
| type G = MatchOrNever<never, never>; // => never | ||
| type H = MatchOrNever<never, unknown>; // => never | ||
| ``` | ||
|
|
||
| Note that this doesn't regard the identical union/intersection type `T | T` and/or `T & T` as `T` recursively. | ||
| e.g., `{a: 0} | {a: 0}` and/or `{a: 0} & {a: 0}` as `{a: 0}`. | ||
|
|
||
| @example | ||
| ``` | ||
| type IDUnion = MatchOrNever<{a: {b: 0}} | {a: {b: 0}}, {a: {b: 0}}>; // => never | ||
| type A = {a: {b: 0} | {b: 0}}; | ||
| type RecurivelyIDUnion = MatchOrNever<A, {a: {b: 0}}>; // => A | ||
| ``` | ||
| */ | ||
| type MatchOrNever<A, B> = | ||
| [unknown, B] extends [A, never] | ||
| ? A | ||
| // This equality code base below doesn't work if `A` is `unknown` and `B` is `never` case. | ||
| // So this branch should be wrapped to take care of this. | ||
| : (<G>() => G extends A & G | G ? 1 : 2) extends (<G>() => G extends B & G | G ? 1 : 2) | ||
| ? never | ||
| : A; | ||
|
|
||
| /** | ||
| TypeScript's built-in `Exclude` and `ExcludeStrict` in `type-fest` don't distinguish kinds of keys of objects. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {ExcludeStrict} from 'type-fest'; | ||
|
|
||
| type NeverReturned_0 = Exclude<{a: 0} | {readonly a: 0}, {readonly a: 0}>; // => never | ||
| type NeverReturned_1 = ExcludeStrict<{a: 0} | {readonly a: 0}, {readonly a: 0}>; // => never | ||
| ``` | ||
|
|
||
| This `ExcludeExactly` keeps the union objects element if the keys of the first and the second aren't identical. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {ExcludeExactly} from 'type-fest'; | ||
|
|
||
| type ExcludeNever = ExcludeExactly<{a: 0} | {a: 0} | {readonly a: 0}, never>; // => {a: 0} | {a: 0} | {readonly a: 0} | ||
| type ExcludeReadonlyKey = ExcludeExactly<{a: 0} | {readonly a: 0}, {readonly a: 0}>; // => {a: 0} | ||
| type ExcludeKey = ExcludeExactly<{readonly a: 0}, {a: 0}>; // => {readonly a: 0} | ||
| type ExcludeReadonly = ExcludeExactly<{readonly a: 0}, {readonly a: 0}>; // => {readonly a: 0} | ||
|
taiyakihitotsu marked this conversation as resolved.
Outdated
|
||
| type ExcludeSubType = ExcludeExactly<0 | 1 | number, 1>; // => number | ||
| type ExcludeAllSet = ExcludeExactly<0 | 1 | number, number>; // => never | ||
| type ExcludeFromUnknown = ExcludeExactly<unknown, string>; // => unknown | ||
| type ExcludeFromUnknownArray = ExcludeExactly<number[] | unknown[], number[]>; // => unknown[] | ||
| ``` | ||
| */ | ||
| export type ExcludeExactly<UnionU, DeleteT> = | ||
| LastOfUnion<DeleteT> extends infer D | ||
| ? true extends IsNever<D> | ||
| ? UnionU | ||
| : ExcludeExactly<_ExcludeExactly<UnionU, D>, _ExcludeExactly<DeleteT, D>> | ||
| : never; | ||
| type _ExcludeExactly<UnionU, DeleteT> = | ||
| true extends IsAny<DeleteT> | ||
| ? never | ||
| : true extends IsUnknown<DeleteT> | ||
| ? never | ||
| : UnionU extends unknown // Only for union distribution. | ||
| ? MatchOrNever<UnionU, DeleteT> | ||
| : never; | ||
|
|
||
| export {}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type {IsNever} from './is-never.d.ts'; | ||
| import type {UnionToIntersection} from './union-to-intersection.d.ts'; | ||
|
|
||
| /** | ||
| Returns the last element of a union type; otherwise `never` if `never` passed. | ||
| Note that this is non-deterministic because the order of union type is not guaranteed. | ||
|
|
||
| @see https://github.com/microsoft/TypeScript/issues/13298#issuecomment-468375328 | ||
|
|
||
| This can be used to implement a recursive type function that accepts a union type. | ||
| It can detect a termination case using {@link IsNever `IsNever`}. | ||
|
|
||
| @example | ||
| ``` | ||
| import type {LastOfUnion, ExcludeExactly, IsNever} from 'type-fest'; | ||
|
|
||
| export type UnionToTuple<T, L = LastOfUnion<T>> = | ||
| IsNever<T> extends false | ||
| ? [...UnionToTuple<ExcludeExactly<T, L>>, L] | ||
| : []; | ||
| ``` | ||
|
|
||
| @example | ||
| ``` | ||
| import type {LastOfUnion} from 'type-fest'; | ||
|
|
||
| type Last = LastOfUnion<1 | 2 | 3>; | ||
| //=> 3 | ||
|
|
||
| type LastNever = LastOfUnion<never>; | ||
| //=> never | ||
| ``` | ||
| */ | ||
| export type LastOfUnion<T> = | ||
| true extends IsNever<T> | ||
| ? never | ||
| : UnionToIntersection<T extends any ? () => T : never> extends () => (infer R) | ||
| ? R | ||
| : never; | ||
|
|
||
| export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {ExcludeExactly} from '../index.d.ts'; | ||
|
|
||
| expectType<number>({} as ExcludeExactly<0 | 1 | number, '1'>); | ||
| expectType<never>({} as ExcludeExactly<0 | 1 | number, number>); | ||
| expectType<string>({} as ExcludeExactly<'0' | '1' | string, '1'>); | ||
| expectType<never>({} as ExcludeExactly<'0' | '1' | string, string>); | ||
|
|
||
| // `{readonly a: t}` should not be equal to `{a: t}` because of assignability. | ||
| expectType<{a: 0}>({} as ExcludeExactly<{a: 0} | {readonly a: 0}, {readonly a: 0}>); | ||
| expectType<{readonly a: 0}>({} as ExcludeExactly<{readonly a: 0}, {a: 0}>); | ||
| expectType<never>({} as ExcludeExactly<{readonly a: 0}, {readonly a: 0}>); | ||
|
|
||
| // `never` does nothing. | ||
| expectType<0 | 1 | 2>({} as ExcludeExactly<0 | 1 | 2, never>); | ||
| expectType<never>({} as ExcludeExactly<never, never>); | ||
|
|
||
| // `unknown` cannot be excluded like `unknown\T` in any cases. | ||
| expectType<unknown>({} as ExcludeExactly<unknown, string>); | ||
| expectType<[unknown]>({} as ExcludeExactly<[unknown], [number]>); | ||
| expectType<unknown[]>({} as ExcludeExactly<unknown[], number[]>); | ||
| expectType<{a: unknown}>({} as ExcludeExactly<{a: unknown}, {a: number}>); | ||
| expectType<unknown[]>({} as ExcludeExactly<number[] | unknown[], number[]>); | ||
|
|
||
| // `unknown` and `any` exclude themselves. | ||
| expectType<never>({} as ExcludeExactly<unknown, unknown>); | ||
| expectType<never>({} as ExcludeExactly<unknown, any>); | ||
| expectType<never>({} as ExcludeExactly<any, any>); | ||
| expectType<never>({} as ExcludeExactly<any, unknown>); | ||
|
|
||
| // `unknown` and `any` exclude other types. | ||
| expectType<never>({} as ExcludeExactly<string | number, unknown>); | ||
| expectType<never>({} as ExcludeExactly<string | number, any>); | ||
|
|
||
| // Union | ||
| expectType<2>({} as ExcludeExactly<0 | 1 | 2, 0 | 1>); | ||
| expectType<never>({} as ExcludeExactly<0 | 1 | 2, 0 | 1 | 2>); | ||
| expectType<{readonly a?: 0}>({} as ExcludeExactly<{a: 0} | {readonly a: 0} | {a?: 0} | {readonly a?: 0}, {a: 0} | {readonly a: 0} | {a?: 0}>); | ||
| expectType<never>({} as ExcludeExactly<{a: 0} | {readonly a: 0} | {a?: 0} | {readonly a?: 0}, {a: 0} | {readonly a: 0} | {a?: 0} | {readonly a?: 0}>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {UniqueUnionDeep} from '../../source/internal/type.d.ts'; | ||
|
|
||
| // The returns of `UniqueUnionDeep` are expected to be equal to `UniqueUnion`, if there's no recursive object type in the passed union type. | ||
| // That's why those tests are pasted from 'test-d/internal/union-unique.ts'. | ||
|
|
||
| expectType<never>({} as UniqueUnionDeep<never>); | ||
| expectType<string>({} as UniqueUnionDeep<string>); | ||
| expectType<{a: 0}>({} as UniqueUnionDeep<{a: 0}>); | ||
| expectType<unknown>({} as UniqueUnionDeep<unknown>); | ||
| expectType<any>({} as UniqueUnionDeep<any>); | ||
| expectType<[unknown]>({} as UniqueUnionDeep<[unknown]>); | ||
| expectType<[any]>({} as UniqueUnionDeep<[any]>); | ||
|
|
||
| expectType<{a: 0} | {a: 1}>({} as UniqueUnionDeep<{a: 0} | {a: 0} | {a: 0} | {a: 1}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `{a: t}` isn't excluded by `{a: T}` even if `T` includes `t`. | ||
| expectType<{a: 0} | {a: 1} | {a: number}>({} as UniqueUnionDeep<{a: 0} | {a: 0} | {a: 0} | {a: 1} | {a: number}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `readonly`, `optional`, both, and general object key shouldn't be mutually equal. | ||
| expectType<{a: number} | {readonly a: number} | {a?: number} | {readonly a?: number}>({} as UniqueUnionDeep<{a: number} | {readonly a: number} | {a?: number} | {readonly a?: number} | {a: number} | {readonly a: number} | {a?: number} | {readonly a?: number}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // Empty tuple isn't removed by `T[]`. | ||
| expectType<[0, 1, 2] | [0, 1] | [] | number[]>({} as UniqueUnionDeep<[0, 1, 2] | [0, 1, 2] | [0, 1, 2] | [0, 1] | [] | number[]>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `T[]` doesn't delete tuples of `[t, ...t]` even if `T` includes `t`. | ||
| expectType<[0, 1, 2] | ['0', unknown] | [0, unknown]>({} as UniqueUnionDeep<[0, 1, 2] | [0, 1, 2] | [0, 1, 2] | [0, unknown] | ['0', unknown]>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `[u, ...u]` doesn't delete tuple of `[v, ...y]` even if `u` includes `v`. | ||
| expectType<[0, 1, 2] | ['0', unknown] | [0, unknown] | number[]>({} as UniqueUnionDeep<[0, 1, 2] | [0, 1, 2] | [0, 1, 2] | [0, unknown] | ['0', unknown] | number[]>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| expectType<{z: {a: {aa: 0}} | {b: 0}; x: '1'}>({} as UniqueUnionDeep<{z: {a: {aa: 0} | {aa: 0}} | {a: {aa: 0} | {aa: 0}} | {b: 0}; x: '1'}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
| expectType<{z: {a: 0}; x: '1'} | {z: {a: 0}; x: '2'}>({} as UniqueUnionDeep<{z: {a: 0} | {a: 0}; x: '1'} | {z: {a: 0} | {a: 0}; x: '2'}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {expectType} from 'tsd'; | ||
| import type {UniqueUnion} from '../../source/internal/type.d.ts'; | ||
|
|
||
| expectType<never>({} as UniqueUnion<never>); | ||
| expectType<string>({} as UniqueUnion<string>); | ||
| expectType<{a: 0}>({} as UniqueUnion<{a: 0}>); | ||
| expectType<unknown>({} as UniqueUnion<unknown>); | ||
| expectType<any>({} as UniqueUnion<any>); | ||
| expectType<[unknown]>({} as UniqueUnion<[unknown]>); | ||
| expectType<[any]>({} as UniqueUnion<[any]>); | ||
|
|
||
| expectType<{a: 0} | {a: 1}>({} as UniqueUnion<{a: 0} | {a: 0} | {a: 0} | {a: 1}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `{a: t}` isn't excluded by `{a: T}` even if `T` includes `t`. | ||
| expectType<{a: 0} | {a: 1} | {a: number}>({} as UniqueUnion<{a: 0} | {a: 0} | {a: 0} | {a: 1} | {a: number}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `readonly`, `optional`, both, and general object key shouldn't be mutually equal. | ||
| expectType<{a: number} | {readonly a: number} | {a?: number} | {readonly a?: number}>({} as UniqueUnion<{a: number} | {readonly a: number} | {a?: number} | {readonly a?: number} | {a: number} | {readonly a: number} | {a?: number} | {readonly a?: number}>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // Empty tuple isn't removed by `T[]`. | ||
| expectType<[0, 1, 2] | [0, 1] | [] | number[]>({} as UniqueUnion<[0, 1, 2] | [0, 1, 2] | [0, 1, 2] | [0, 1] | [] | number[]>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `T[]` doesn't delete tuples of `[t, ...t]` even if `T` includes `t`. | ||
| expectType<[0, 1, 2] | ['0', unknown] | [0, unknown]>({} as UniqueUnion<[0, 1, 2] | [0, 1, 2] | [0, 1, 2] | [0, unknown] | ['0', unknown]>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents | ||
|
|
||
| // `[u, ...u]` doesn't delete tuples of `[v, ...y]` even if `u` includes `v`. | ||
| expectType<[0, 1, 2] | ['0', unknown] | [0, unknown] | number[]>({} as UniqueUnion<[0, 1, 2] | [0, 1, 2] | [0, 1, 2] | [0, unknown] | ['0', unknown] | number[]>); // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.