Skip to content

Commit 3cc8c88

Browse files
committed
feat: add some comments for types
1 parent 665e429 commit 3cc8c88

File tree

12 files changed

+134
-25
lines changed

12 files changed

+134
-25
lines changed

src/types/DeepDateToString.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* A utility type that recursively converts all `Date` types within a nested object or array to `string`.
3+
*
4+
* - If `T` is an array of objects, the type processes each element recursively.
5+
* - If `T` is a `Date`, it is converted to `string`.
6+
* - If `T` is an object, each key is checked recursively for `Date` types or nested objects.
7+
*/
18
export type DeepDateToString<T> =
29
T extends Array<infer I extends object>
310
? Array<DeepDateToString<I>>

src/types/DeepStrictObjectKeys.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ type __DeepStrictObjectKeys<
3232
: never;
3333

3434
/**
35-
* @title 중첩된 객체 혹은 배열의 모든 키를 표기하는 타입.
35+
* @title Type for Listing All Keys of Nested Objects or Arrays.
3636
*
37-
* 중첩된 객체의 모든 키를 뽑는 타입으로, 만약 중첩된 객체가 있을 경우 점 기호를 기준으로 객체를 표현한다.
38-
* 배열인 경우에는 `[*]` 기호를 이용하여 표기한다.
37+
* A type that extracts all keys of a nested object. If the object contains nested properties,
38+
* the keys are represented using dot notation. For arrays, the keys are represented using
39+
* the `[*]` symbol.
3940
*
4041
* ```ts
4142
* type Example1 = DeepStrictObjectKeys<{ a: { b: 1; c: 2 } }>; // "a" | "a.b" | "a.c"

src/types/DeepStrictOmit.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ type _DeepStrictOmit<T extends object, K extends DeepStrictObjectKeys<T>> =
3737
: ____DeepStrictOmit<T, K>;
3838

3939
/**
40-
* @title 인터페이스에서 특정 키를 제거하는 타입.
41-
* {@link DeepStrictObjectKeys} 을 이용해서 제거할 키를 고를 수 있다.
40+
* @title Type for Removing Specific Keys from an Interface.
4241
*
42+
* The `DeepStrictOmit<T, K>` type creates a new type by excluding properties
43+
* corresponding to the key `K` from the object `T`, while preserving the nested structure.
44+
* It enables precise omission of keys even in deeply nested objects or arrays.
45+
*
46+
* {@link DeepStrictObjectKeys} can be used to determine valid keys for omission,
47+
* including nested keys represented with dot notation (`.`) and array indices represented with `[*]`.
48+
*
49+
* Example Usage:
4350
* ```ts
44-
* type Example1 = DeepStrictOmit<{ a: { b: 1; c: 2 } }, "a.b">;
45-
* type Example2 = DeepStrictOmit<{ a: { b: 1; c: { d: number }[] } }, "a.c[*].d">;
46-
* type Example3 = DeepStrictOmit<{ a: 1 }[], "[*].a">;
51+
* type Example1 = DeepStrictOmit<{ a: { b: 1; c: 2 } }, "a.b">; // { a: { c: 2 } }
52+
* type Example2 = DeepStrictOmit<{ a: { b: 1; c: { d: number }[] } }, "a.c[*].d">; // { a: { b: 1; c: {}[] } }
53+
* type Example3 = DeepStrictOmit<{ a: 1 }[], "[*].a">; // {}[]
4754
* ```
4855
*/
4956
export type DeepStrictOmit<T extends object, K extends DeepStrictObjectKeys<DeepStrictUnbrand<T>>> = _DeepStrictOmit<

src/types/DeepStrictPick.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import type { RemoveAfterDot } from './RemoveAfterDot';
55
import type { RemoveLastProperty } from './RemoveLastProperty';
66

77
/**
8-
* @title 인터페이스에서 특정 키만을 뽑는 타입.
9-
* {@link DeepStrictObjectKeys} 을 이용해서 뽑을 키를 고를 수 있다.
8+
* @title Type for Selecting Specific Keys from an Interface.
109
*
10+
* The `DeepStrictPick<T, K>` type creates a new type by selecting only the properties
11+
* corresponding to the key `K` from the object `T`, while preserving the nested structure.
12+
* This type allows safely selecting specific keys, even from deeply nested objects or arrays.
13+
*
14+
* {@link DeepStrictObjectKeys} can be used to determine valid keys for selection,
15+
* including nested keys represented with dot notation (`.`) and array indices represented with `[*]`.
16+
*
17+
* Example Usage:
1118
* ```ts
12-
* type Example1 = DeepStrictPick<{ a: { b: 1; c: 2 } }, "a.b">;
13-
* type Example2 = DeepStrictPick<{ a: { b: 1; c: { d: number }[] } }, "a.c[*].d">;
14-
* type Example3 = DeepStrictPick<{ a: 1 }[], "[*].a">;
19+
* type Example1 = DeepStrictPick<{ a: { b: 1; c: 2 } }, "a.b">; // { a: { b: 1 } }
20+
* type Example2 = DeepStrictPick<{ a: { b: 1; c: { d: number }[] } }, "a.c[*].d">; // { a: { c: { d: number }[] } }
21+
* type Example3 = DeepStrictPick<{ a: 1 }[], "[*].a">; // { a: 1 }[]
1522
* ```
1623
*/
1724
export type DeepStrictPick<T extends object, K extends DeepStrictObjectKeys<T>> = DeepStrictOmit<

src/types/DeepStrictUnbrand.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ type Unbrand<T extends Primitive & Record<any, any>> = T extends string & Record
1313
? Extract<undefined, Omit<T, any>>
1414
: T;
1515

16+
/**
17+
* @title Type for Recursively Removing Branding Types.
18+
*
19+
* The `DeepStrictUnbrand<T>` type recursively processes the type `T` to remove any branding
20+
* that may have been added to primitive types or object properties. Branding often occurs when
21+
* extending or augmenting primitive types for type safety, and this type "unbrands" them, restoring
22+
* the original primitive type (e.g., `string`, `number`, `boolean`, etc.).
23+
*
24+
* The helper type `Unbrand<T>` is used to handle primitive types and their respective branding,
25+
* by stripping off any additional properties that may have been added to them.
26+
*
27+
* The recursion goes through:
28+
* - Arrays: It recursively processes elements of the array, maintaining deep unbranding.
29+
* - Objects: It recursively processes each key in the object, unbranding any branded properties.
30+
* - Primitives: It removes branding from primitive types (`string`, `number`, `boolean`, `symbol`, `null`, `undefined`).
31+
* - Dates: The `Date` type is preserved as it is.
32+
*
33+
* Example Usage:
34+
* ```ts
35+
* type Example1 = DeepStrictUnbrand<{ a: string & { __brand: 'unique' } }>; // { a: string }
36+
* type Example2 = DeepStrictUnbrand<{ a: { b: number & { __brand: 'id' } } }>; // { a: { b: number } }
37+
* type Example3 = DeepStrictUnbrand<Array<string & { __brand: 'email' }>>; // Array<string>
38+
* ```
39+
*/
1640
export type DeepStrictUnbrand<T> =
1741
T extends Array<Date>
1842
? Array<Date>

src/types/ElementOf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
2-
* @title 배열의 요소 타입을 추론하는 타입.
2+
* @title Type for Inferring the Element Type of an Array.
33
*/
44
export type ElementOf<T extends Array<any>> = T extends Array<infer Element> ? Element : never;

src/types/Equal.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/**
2-
* 구현 방법
2+
* Implementation method
33
*/
44
type Expression<X> = <T>() => T extends X ? 1 : 2;
55

66
/**
7-
* @title 두 타입이 동일한 타입인지 확인하기 위한 타입
7+
* @title Type for Checking if Two Types are Equal.
8+
*
9+
* The `Equal<X, Y>` type uses conditional types and a helper type `Expression<X>`
10+
* to determine if two types `X` and `Y` are the same. It returns `true` if they are
11+
* equal, and `false` otherwise.
812
*/
913
export type Equal<X, Y> = Expression<X> extends Expression<Y> ? true : false;

src/types/GetMember.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
/**
2-
* @title 어떤 키로부터 멤버만을 조회하는 타입.
2+
* @title Type for Retrieving Members from a Specific Key.
33
*
4+
* The `GetMember<T, O>` type extracts the part of the key `T` after the given prefix key `O`.
5+
* If `T` starts with `O`, it returns the remaining portion of the key after `O`.
6+
*
7+
* Example Usage:
48
* ```ts
5-
* type b = GetMember<"a.b", "a">;
6-
* type b_c = GetMember<"a.b.c", "a">;
9+
* type b = GetMember<"a.b", "a">; // "b"
10+
* type b_c = GetMember<"a.b.c", "a">; // "b.c"
711
* ```
812
*/
913
export type GetMember<T extends string, O extends string> = T extends `${O}.${infer Rest}` ? Rest : never;
1014

1115
/**
12-
* @title 어떤 키로부터 요소 멤버의 키를 조회하는 타입.
16+
* @title Type for Retrieving the Key of an Element Member from a Specific Key.
17+
*
18+
* The `GetElementMember<T, First>` type handles both regular keys and array element keys.
19+
* If the key `T` represents an array element (denoted by `[*]`), it returns the key after the array element.
20+
* Otherwise, it falls back to using `GetMember<T, First>` to retrieve the remaining portion of the key.
21+
*
22+
* Example Usage:
23+
* ```ts
24+
* type ElementKey = GetElementMember<"a[*].b", "a">; // "b"
25+
* type NestedKey = GetElementMember<"a.b.c", "a">; // "b.c"
26+
* ```
1327
*/
1428
export type GetElementMember<T extends string, First extends string> = T extends `${First}[*].${infer Rest}`
1529
? Rest

src/types/GetStrictObjectLastKeys.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ type __DeepStrictObjectKeys<
3030
: never
3131
: never;
3232

33+
/**
34+
* @title Type for Extracting the Last Level Keys from Nested Objects, Including Array Elements.
35+
*
36+
* The `DeepStrictObjectLastKeys<T, Joiner, P>` type extracts the keys from the last level of a nested object `T`,
37+
* with support for arrays. It returns keys with array indices (`[*]`) and object keys using a custom separator defined
38+
* by the `Joiner` object.
39+
* - For arrays, it appends array indices (`[*]`) followed by the key of the element.
40+
* - For objects, it recursively traverses the nested structure and appends the last level keys.
41+
*
42+
* Example Usage:
43+
* ```ts
44+
* type Example1 = DeepStrictObjectLastKeys<{ a: { b: { c: number[] } } }>; // "a.b.c"
45+
* type Example2 = DeepStrictObjectLastKeys<{ a: { b: number[]; c: { d: string }[] } }>; // "a.b" | "a.c" | "a.c[*].d"
46+
* ```
47+
*/
3348
export type DeepStrictObjectLastKeys<
3449
T extends object,
3550
Joiner extends { array: string; object: string } = {

src/types/GetType.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ type ToObject<T> = Allow<T, object>;
1616

1717
/**
1818
* @title The type that pulls out the type of a particular key on an interface.
19-
* @template {T}
20-
* @template {K}
2119
*
20+
* This type extracts the type of a specific key from a nested object,
21+
* supporting arrays and deeply nested keys. It uses `DeepStrictObjectKeys`
22+
* to handle the extraction of keys and correctly resolves the type for the given key.
23+
*
24+
* - If the key points to a primitive value, the type is returned directly.
25+
* - If the key points to an array, the type of the array elements is resolved.
26+
* - It supports nested keys using `.` notation to handle deep objects and arrays.
27+
*
28+
* @template T The interface type.
29+
* @template K The key string, which can represent a nested key path.
30+
*
31+
* Example usage:
2232
* ```ts
2333
* type Example1 = GetType<{ a: { b: { c: number } } }, "a.b">; // { c: number }
24-
* type Example = GetType<{ a: { b: { c: number } } }, "a.b.c">; // number
34+
* type Example2 = GetType<{ a: { b: { c: number } } }, "a.b.c">; // number
2535
* ```
2636
*/
2737
export type GetType<T extends object, K extends DeepStrictObjectKeys<T>> =

0 commit comments

Comments
 (0)