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