Skip to content

Commit

Permalink
feat(Conditionals): Add MutuallyExtend
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWrexes committed Jun 1, 2023
1 parent 530c4ea commit 831563b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
30 changes: 27 additions & 3 deletions tests/Conditionals.test.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { Expect, Equal, ExpectTrue, ExpectFalse } from '@type-challenges/utils'
import { Expect, Equal, ExpectTrue, ExpectFalse, MergeInsertions } from '@type-challenges/utils'

import { Extends, If, Not, And, Or, Xor, NeverIf, NeverIfNot, IsNever, IfNever } from '../types'
import {
Extends,
If,
Not,
And,
Or,
Xor,
NeverIf,
NeverIfNot,
IsNever,
IfNever,
MutuallyExtend,
} from '../types'

// prettier-ignore
type __TEST__ = {
// prettier-ignore
If: [
Expand All @@ -22,6 +33,19 @@ type __TEST__ = {
ExpectFalse<Extends<number, 23 | 42>>,
ExpectFalse<Extends<string, 'sauce'>>
]
MutuallyExtend: [
ExpectTrue<MutuallyExtend<string, string>>,
ExpectTrue<MutuallyExtend<number | string, string | number>>,
ExpectTrue<MutuallyExtend<{ oui: string; non: number }, { oui: string } & { non: number }>>,
ExpectTrue<
MutuallyExtend<
MergeInsertions<{ oui: string } & { non: number }>,
{ oui: string; non: number }
>
>,
ExpectFalse<MutuallyExtend<{ clique: 'salope'; 92: 'easy' }, { 92: 'easy' }>>
]
// prettier-ignore
Not: [
ExpectTrue<Not<false>>,
ExpectFalse<Not<true>>,
Expand Down
2 changes: 2 additions & 0 deletions types/Conditionals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export type Extends<Type, ToExtend>
? true
: false
export type NotExtends<Type, ToExtend> = Not<Extends<Type, ToExtend>>
export type MutuallyExtend<T1, T2> = If<Extends<T1, T2>, Extends<T2, T1>, false>

export type IfExtends<Type, ToExtend, OnExtends, OnNotExtends = never> = If<
export type Not<Test extends boolean> = If<Test, false, true>
export type And<A extends boolean, B extends boolean> = If<A, If<B, true, false>, false>
export type Or<A extends boolean, B extends boolean> = If<A, true, If<B, true, false>>
Expand Down

0 comments on commit 831563b

Please sign in to comment.