forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-is-equal.ts
More file actions
95 lines (76 loc) · 4.92 KB
/
simple-is-equal.ts
File metadata and controls
95 lines (76 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {expectType} from 'tsd';
import type {TupleOf} from '../../index.d.ts';
import type {SimpleIsEqual} from '../../source/internal/index.d.ts';
expectType<false>({} as SimpleIsEqual<number, string>);
expectType<true>({} as SimpleIsEqual<1, 1>);
expectType<false>({} as SimpleIsEqual<'A', 'B'>);
expectType<true>({} as SimpleIsEqual<'foo', 'foo'>);
expectType<false>({} as SimpleIsEqual<true, false>);
expectType<true>({} as SimpleIsEqual<false, false>);
expectType<false>({} as SimpleIsEqual<any, number>);
expectType<false>({} as SimpleIsEqual<'', never>);
expectType<true>({} as SimpleIsEqual<any, any>);
expectType<true>({} as SimpleIsEqual<never, never>);
expectType<false>({} as SimpleIsEqual<any, never>);
expectType<false>({} as SimpleIsEqual<never, any>);
expectType<false>({} as SimpleIsEqual<any, unknown>);
// `IsEqual` returns `false`, `SimpleIsEqual` returns `true`.
expectType<true>({} as SimpleIsEqual<never, unknown>);
// `IsEqual` returns `false`, `SimpleIsEqual` returns `true`.
expectType<true>({} as SimpleIsEqual<unknown, never>);
expectType<false>({} as SimpleIsEqual<[never], [unknown]>);
expectType<false>({} as SimpleIsEqual<[unknown], [never]>);
expectType<false>({} as SimpleIsEqual<[any], [never]>);
expectType<true>({} as SimpleIsEqual<[any], [any]>);
expectType<true>({} as SimpleIsEqual<[never], [never]>);
expectType<false>({} as SimpleIsEqual<1 | 2, 1>);
expectType<false>({} as SimpleIsEqual<1 | 2, 2 | 3>);
expectType<true>({} as SimpleIsEqual<1 | 2, 2 | 1>);
expectType<false>({} as SimpleIsEqual<boolean, true>);
expectType<true>({} as SimpleIsEqual<{a: 1}, {a: 1}>);
expectType<false>({} as SimpleIsEqual<{a: 1}, {a?: 1}>);
expectType<false>({} as SimpleIsEqual<{a: 1}, {readonly a: 1}>);
expectType<true>({} as SimpleIsEqual<[], []>);
expectType<true>({} as SimpleIsEqual<readonly [], readonly []>);
expectType<false>({} as SimpleIsEqual<readonly [], []>);
expectType<true>({} as SimpleIsEqual<number[], number[]>);
expectType<true>({} as SimpleIsEqual<readonly number[], readonly number[]>);
expectType<false>({} as SimpleIsEqual<readonly number[], number[]>);
expectType<true>({} as SimpleIsEqual<[string], [string]>);
expectType<false>({} as SimpleIsEqual<[string], [string, number]>);
expectType<false>({} as SimpleIsEqual<[0, 1] | [0, 2], [0, 2]>);
type LongTupleNumber = TupleOf<50, 0>;
expectType<true>({} as SimpleIsEqual<LongTupleNumber, LongTupleNumber>);
type ReadonlyLongTupleNumber = Readonly<TupleOf<50, 0>>;
expectType<true>({} as SimpleIsEqual<ReadonlyLongTupleNumber, ReadonlyLongTupleNumber>);
expectType<false>({} as SimpleIsEqual<ReadonlyLongTupleNumber, LongTupleNumber>);
// Missing all generic parameters.
// @ts-expect-error
type A = SimpleIsEqual;
// Missing `Y` generic parameter.
// @ts-expect-error
type B = SimpleIsEqual<number>;
// Test for issue https://github.com/sindresorhus/type-fest/issues/537
type UnionType = SimpleIsEqual<{a: 1} | {a: 1}, {a: 1}>; // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents
expectType<UnionType>(true);
type IntersectionType = SimpleIsEqual<{a: 1} & {a: 1}, {a: 1}>; // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents
expectType<IntersectionType>(true);
// Test for PR https://github.com/sindresorhus/type-fest/pull/1231
type BranchOnWrappedTupleMatches<Tpl> = (Tpl extends [[0, 2]] ? 'Foo' : 'Bar');
type BranchOnWrappedTupleDoesNotMatch<Tpl> = (Tpl extends [[0, 1]] ? 'Foo' : 'Bar');
type BranchOnTupleMatches<Tpl> = (Tpl extends [0, 2] ? 'Foo' : 'Bar');
type BranchOnTupleDoesNotMatch<Tpl> = (Tpl extends [0, 1] ? 'Foo' : 'Bar');
declare const equalWrappedTupleIntersectionToBeNeverAndNever: SimpleIsEqual<(BranchOnWrappedTupleMatches<[[0, 2]]> & BranchOnWrappedTupleDoesNotMatch<[[0, 2]]>), never>;
expectType<true>(equalWrappedTupleIntersectionToBeNeverAndNever);
// `IsEqual` returns `false`, `SimpleIsEqual` returns `true`.
declare const equalWrappedTupleIntersectionToBeNeverAndNeverExpanded: [0, 2] extends infer Tpl ? SimpleIsEqual<(BranchOnWrappedTupleMatches<[Tpl]> & BranchOnWrappedTupleDoesNotMatch<[Tpl]>), never> : never;
expectType<false>(equalWrappedTupleIntersectionToBeNeverAndNeverExpanded);
declare const equalTupleIntersectionToBeNeverAndNever: SimpleIsEqual<(BranchOnTupleMatches<[0, 2]> & BranchOnTupleDoesNotMatch<[0, 2]>), never>;
expectType<true>(equalTupleIntersectionToBeNeverAndNever);
declare const equalTupleIntersectionToBeNeverAndNeverExpanded: [0, 2] extends infer Tpl ? SimpleIsEqual<(BranchOnTupleMatches<Tpl> & BranchOnTupleDoesNotMatch<Tpl>), never> : never;
expectType<true>(equalTupleIntersectionToBeNeverAndNeverExpanded);
declare const equalTupleIntersectionAndTuple: SimpleIsEqual<[{a: 1}] & [{a: 1}], [{a: 1}]>; // eslint-disable-line @typescript-eslint/no-duplicate-type-constituents
expectType<true>(equalTupleIntersectionAndTuple);
// Test for Issue https://github.com/sindresorhus/type-fest/issues/1305
type Assignability<T, U, _V extends SimpleIsEqual<T, U>> = any;
type TestAssignability<T> = Assignability<T, T, true>;