-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): DistributiveOmit 타입 추가 (#647)
- Loading branch information
Showing
11 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@modern-kit/types': minor | ||
--- | ||
|
||
feat(types): DistributiveOmit 타입 추가 - @ssi02014 |
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
packages/types/src/DistributiveOmit/DistributiveOmit.spec.ts
This file contains 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,16 @@ | ||
import { describe, it, expectTypeOf } from 'vitest'; | ||
import { DistributiveOmit } from '.'; | ||
|
||
describe('DistributiveOmit', () => { | ||
it('유니온 타입에서 각각의 타입에 대해 Omit을 적용한 타입을 반환해야 합니다. ', () => { | ||
const obj = { a: 1, b: 2 } as | ||
| { a: number; b: number } | ||
| { a: string; c: number }; | ||
|
||
const result: DistributiveOmit<typeof obj, 'a'> = { b: 2 }; | ||
|
||
expectTypeOf(result as { b: number } | { c: number }).toEqualTypeOf< | ||
{ b: number } | { c: number } | ||
>(); | ||
}); | ||
}); |
This file contains 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,20 @@ | ||
/** | ||
* @description 유니온 타입에서 각각의 타입에 대해 `Omit`을 적용하는 타입입니다. | ||
* | ||
* `조건부 타입`을 사용하여 `분배 법칙`처럼 동작합니다. | ||
* | ||
* @template T - 분배 대상이 되는 유니온 타입 | ||
* @template K - 제거할 프로퍼티 키 | ||
* | ||
* @example | ||
* type Union = { a: string } | { b: number } | ||
* type Result = DistributiveOmit<Union, 'a'> | ||
* | ||
* // 동작 원리와 순서 | ||
* // 1. Result = DistributiveOmit<Union, 'a'> | ||
* // 2. Result = Omit<{ a: string }, 'a'> | Omit<{ b: number }, 'a'> | ||
* // 3. Result = {} | { b: number } | ||
*/ | ||
export type DistributiveOmit<T, K extends PropertyKey> = T extends any | ||
? Omit<T, K> | ||
: never; |
This file contains 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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
/** | ||
* @description Map의 타입에서 키와 값의 타입을 추출하여 튜플로 반환하는 타입입니다. | ||
* | ||
* @template T - 타입을 추출할 Map 타입 | ||
* @returns 입력된 타입이 Map인 경우 [키 타입, 값 타입] 형태의 튜플을 반환하고, Map이 아닌 경우 never를 반환합니다. | ||
* @example | ||
* type StringNumberMap = Map<string, number>; | ||
* type Extracted = ExtractMapType<StringNumberMap>; // [string, number] | ||
*/ | ||
export type ExtractMapType<T> = T extends Map<infer K, infer V> | ||
? [K, V] | ||
: never; |
This file contains 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 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 |
---|---|---|
@@ -1 +1,15 @@ | ||
/** | ||
* @description Set 타입에서 내부 요소의 타입을 추출하는 타입입니다. | ||
* | ||
* @template T - 타입을 추출할 Set 타입 | ||
* @returns Set의 내부 요소 타입을 반환합니다. Set이 아닌 경우 never를 반환합니다. | ||
* | ||
* @example | ||
* ```typescript | ||
* type StringSet = Set<string>; | ||
* type Result = ExtractSetType<StringSet>; // string | ||
* | ||
* type NotASet = ExtractSetType<number>; // never | ||
* ``` | ||
*/ | ||
export type ExtractSetType<T> = T extends Set<infer U> ? U : never; |
This file contains 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 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,15 @@ | ||
/** | ||
* @description 읽기 전용 배열을 나타내는 타입입니다. | ||
* 기본 배열 타입은 더 좁은 타입의 읽기 전용 배열과 호환됩니다. | ||
* | ||
* @template T - 배열의 요소 타입 | ||
* @returns 읽기 전용 배열 타입 | ||
* @example | ||
* ```typescript | ||
* type MyArray = ReadonlyArray<number>; | ||
* | ||
* const myArray: MyArray = [1, 2, 3]; // 허용 | ||
* const myArray2: MyArray = [1, 2, 3] as const; // 허용 | ||
* ``` | ||
*/ | ||
export type ReadonlyArray<T> = readonly T[]; |
This file contains 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 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