Skip to content
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

feat(UnionToIntersection): Add UniontoIntersection #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ export default defineConfig({
text: 'TupleToUnion',
link: '/reference/utilities/TupleToUnion',
},
{
text: 'UnionToIntersection',
link: '/reference/utilities/UnionToIntersection',
},
],
},
],
Expand Down
4 changes: 4 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export default defineConfig({
text: 'TupleToUnion',
link: '/ko/reference/utilities/TupleToUnion',
},
{
text: 'UnionToIntersection',
link: '/ko/reference/utilities/UnionToIntersection',
},
],
},
],
Expand Down
65 changes: 65 additions & 0 deletions docs/ko/reference/utilities/UnionToIntersection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# UnionToIntersection\<U>

## 개요

분배 조건부 타입을 사용하여 유니온 타입을 교차 타입으로 변환해요.

## 문법

```ts
type InternalUnionToIntersection<U> = (
U extends unknown ? (x: U) => void : never
) extends (x: inter I) => void
? I & U
: never;

type SimplifyIfRecord<T> =
T extends Record<PropertyKey, unknown> ? Simplify<T> : T;

type UnionToIntersection<U> =
IsNever<U> extends true
? never
: SimplifyIfRecord<InternalUnionToIntersection<U>>;
```

- **U**: 교차 타입으로 변환될 유니온 타입이에요.

## 예제

```ts
// T0: Object literal union
type T0 = { a: number } | { b: string };
type E0 = UnionToIntersection<T0>; // { a: number; b: string; }

// T1: Union of object literal and simple type
type T1 = { a: number } | 'A';
type E1 = UnionToIntersection<T1>; // { a: number } & 'A'

// T2: Combination of object literal, function, and tuple
type T2 = { a: number } | (() => void) | [number];
type E2 = UnionToIntersection<T2>; // { a: number } & (() => void) & [number]

// T3: Union of function types
type T3 = (() => string) | ((x: number) => number);
type E3 = UnionToIntersection<T3>; // (() => string) & ((x: number) => number)

// T4: Union of tuple types
type T4 = [number] | [string];
type E4 = UnionToIntersection<T4>; // [number] & [string]

// T5: Nested object literal
type T5 = { a: { b: string } } | { a: { c: number } };
type E5 = UnionToIntersection<T5>; // { a: { b: string } & { c: number } }

// T6: Union of object literal and tuple
type T6 = { a: number } | [string];
type E6 = UnionToIntersection<T6>; // { a: number } & [string]

// T7: Combination of object literal, primitive type, and tuple
type T7 = { a: string } | 'B' | [number];
type E7 = UnionToIntersection<T7>; // { a: string } & 'B' & [number]

// T8: Simple union type (not an object)
type T8 = 'A' | 'B' | 'C' | 'D' | 'E';
type E8 = UnionToIntersection<T8>; // never
```
65 changes: 65 additions & 0 deletions docs/reference/utilities/UnionToIntersection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# UnionToIntersection\<U>

## Overview

Convert a union type to an intersection type using distributive conditional types.

## Syntax

```ts
type InternalUnionToIntersection<U> = (
U extends unknown ? (x: U) => void : never
) extends (x: inter I) => void
? I & U
: never;

type SimplifyIfRecord<T> =
T extends Record<PropertyKey, unknown> ? Simplify<T> : T;

type UnionToIntersection<U> =
IsNever<U> extends true
? never
: SimplifyIfRecord<InternalUnionToIntersection<U>>;
```

- **U**: The union type to be converted to an intersection type.

## Example

```ts
// T0: Object literal union
type T0 = { a: number } | { b: string };
type E0 = UnionToIntersection<T0>; // { a: number; b: string; }

// T1: Union of object literal and simple type
type T1 = { a: number } | 'A';
type E1 = UnionToIntersection<T1>; // { a: number } & 'A'

// T2: Combination of object literal, function, and tuple
type T2 = { a: number } | (() => void) | [number];
type E2 = UnionToIntersection<T2>; // { a: number } & (() => void) & [number]

// T3: Union of function types
type T3 = (() => string) | ((x: number) => number);
type E3 = UnionToIntersection<T3>; // (() => string) & ((x: number) => number)

// T4: Union of tuple types
type T4 = [number] | [string];
type E4 = UnionToIntersection<T4>; // [number] & [string]

// T5: Nested object literal
type T5 = { a: { b: string } } | { a: { c: number } };
type E5 = UnionToIntersection<T5>; // { a: { b: string } & { c: number } }

// T6: Union of object literal and tuple
type T6 = { a: number } | [string];
type E6 = UnionToIntersection<T6>; // { a: number } & [string]

// T7: Combination of object literal, primitive type, and tuple
type T7 = { a: string } | 'B' | [number];
type E7 = UnionToIntersection<T7>; // { a: string } & 'B' & [number]

// T8: Simple union type (not an object)
type T8 = 'A' | 'B' | 'C' | 'D' | 'E';
type E8 = UnionToIntersection<T8>; // never
```
28 changes: 28 additions & 0 deletions source/utilities/UnionToIntersection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { IsNever } from '@/predicate/IsNever';
import { Simplify } from './Simplify';

type InternalUnionToIntersection<U> = (
U extends unknown ? (x: U) => void : never
) extends (x: infer I) => void
? I & U
: never;

type SimplifyIfRecord<T> =
T extends Record<PropertyKey, unknown> ? Simplify<T> : T;

/**
* @description Convert a union type to an intersection type using distributive conditional types.
*
* @template U The union type to be converted to an intersection type.
*
* @returns The intersection type resulting from the conversion of the union type.
*
* @example
* type T0 = { a: number };
* type T1 = { b: string };
* type Result = UnionToIntersection<T0 | T1>; // { a: number; b: string; }
*/
export type UnionToIntersection<U> =
IsNever<U> extends true
? never
: SimplifyIfRecord<InternalUnionToIntersection<U>>;
1 change: 1 addition & 0 deletions source/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export type { StrictExclude } from './StrictExclude';
export type { StrictExtract } from './StrictExtract';
export type { StrictOmit } from './StrictOmit';
export type { TupleToUnion } from './TupleToUnion';
export type { UnionToIntersection } from './UnionToIntersection';
46 changes: 46 additions & 0 deletions test-d/utilities/UnionToIntersection.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { UnionToIntersection } from '@/utilities';
import { expectNever, expectType } from 'tsd';

declare function unionToIntersection<T>(): UnionToIntersection<T>;

// T0: Object literal union
type T0 = { a: number } | { b: string };
expectType<{ a: number; b: string }>(unionToIntersection<T0>());

// T1: Union of object literal and simple type
type T1 = { a: number } | 'A';
expectType<{ a: number } & 'A'>(unionToIntersection<T1>());

// T2: Combination of object literal, function, and tuple
type T2 = { a: number } | (() => void) | [number];
expectType<{ a: number } & (() => void) & [number]>(unionToIntersection<T2>());

// T3: Union of function types
type T3 = (() => string) | ((x: number) => number);
expectType<(() => string) & ((x: number) => number)>(unionToIntersection<T3>());

// T4: Union of tuple types
type T4 = [number] | [string];
expectType<[number] & [string]>(unionToIntersection<T4>());

// T5: Nested object literal
type T5 = { a: { b: string } } | { a: { c: number } };
expectType<{
a: {
b: string;
} & {
c: number;
};
}>(unionToIntersection<T5>());

// T6: Union of object literal and tuple
type T6 = { a: number } | [string];
expectType<{ a: number } & [string]>(unionToIntersection<T6>());

// T7: Combination of object literal, primitive type, and tuple
type T7 = { a: string } | 'B' | [number];
expectType<{ a: string } & 'B' & [number]>(unionToIntersection<T7>());

// T8: Simple union type (not an object)
type T8 = 'A' | 'B' | 'C' | 'D' | 'E';
expectNever(unionToIntersection<T8>());
Loading