Skip to content

Commit d7f82c0

Browse files
committed
docs: add comments for some types
1 parent b0766e0 commit d7f82c0

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

src/types/Equal.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
/**
2-
* Implementation method
2+
* Helper type that creates a generic function signature for type comparison.
3+
* This technique leverages TypeScript's strict function type checking to determine type equality.
4+
*
5+
* @internal
36
*/
47
type Expression<X> = <T>() => T extends X ? 1 : 2;
58

69
/**
710
* @title Type for Checking if Two Types are Equal.
811
*
912
* 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
13+
* to determine if two types `X` and `Y` are exactly the same. It returns `true` if they are
1114
* equal, and `false` otherwise.
15+
*
16+
* This type performs a strict equality check that distinguishes between:
17+
* - `any` vs other types
18+
* - `unknown` vs other types
19+
* - Union types with different members
20+
* - Branded types vs their base types
21+
*
22+
* @template X - The first type to compare
23+
* @template Y - The second type to compare
24+
* @returns `true` if X and Y are exactly the same type, `false` otherwise
25+
*
26+
* @example
27+
* ```typescript
28+
* type Test1 = Equal<string, string>; // true
29+
* type Test2 = Equal<string, number>; // false
30+
* type Test3 = Equal<string | number, string | number>; // true
31+
* type Test4 = Equal<string | number, number | string>; // true (order doesn't matter)
32+
* type Test5 = Equal<{ a: string }, { a: string }>; // true
33+
* type Test6 = Equal<{ a: string }, { a: string; b?: string }>; // false
34+
* type Test7 = Equal<any, string>; // false
35+
* type Test8 = Equal<unknown, any>; // false
36+
* type Test9 = Equal<string & { __brand: 'ID' }, string>; // false (branded types)
37+
* ```
1238
*/
1339
export type Equal<X, Y> = Expression<X> extends Expression<Y> ? true : false;

src/types/GetType.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,50 @@ import type { ElementOf } from './ElementOf';
44
import type { RemoveArraySymbol } from './RemoveArraySymbol';
55

66
/**
7-
* @title Type to infer all value types of generic T
7+
* Helper type that extracts all value types from an object type.
8+
* Similar to `T[keyof T]`, it returns a union of all property value types.
9+
*
10+
* @internal
11+
* @template T - The object type to extract values from
812
*/
913
type ValueOf<T> = T[keyof T];
1014

1115
/**
12-
* @title The type that pulls out the type of a particular key on an interface.
16+
* @title Type for extracting the type at a specific nested path in an object.
1317
*
14-
* This type extracts the type of a specific key from a nested object,
18+
* This type extracts the type of a specific key from a nested object structure,
1519
* supporting arrays and deeply nested keys. It uses `DeepStrictObjectKeys`
16-
* to handle the extraction of keys and correctly resolves the type for the given key.
20+
* to validate the key path and correctly resolves the type for the given key.
21+
*
22+
* Key features:
23+
* - Supports dot notation for nested object access (e.g., "a.b.c")
24+
* - Handles array access with `[*]` notation (e.g., "items[*].name")
25+
* - Type-safe: only accepts valid key paths as defined by `DeepStrictObjectKeys`
26+
* - Recursively resolves nested types through objects and arrays
1727
*
18-
* - If the key points to a primitive value, the type is returned directly.
19-
* - If the key points to an array, the type of the array elements is resolved.
20-
* - It supports nested keys using `.` notation to handle deep objects and arrays.
28+
* @template T - The object type to extract from
29+
* @template K - The key path string (must be a valid key from DeepStrictObjectKeys<T>)
30+
* @returns The type at the specified path, or `never` if the path is invalid
2131
*
22-
* @template T The interface type.
23-
* @template K The key string, which can represent a nested key path.
32+
* @example
33+
* ```typescript
34+
* type Data = {
35+
* user: {
36+
* name: string;
37+
* posts: {
38+
* title: string;
39+
* tags: string[];
40+
* }[];
41+
* };
42+
* };
2443
*
25-
* Example usage:
26-
* ```ts
27-
* type Example1 = GetType<{ a: { b: { c: number } } }, "a.b">; // { c: number }
28-
* type Example2 = GetType<{ a: { b: { c: number } } }, "a.b.c">; // number
44+
* type UserType = GetType<Data, "user">; // { name: string; posts: {...}[] }
45+
* type NameType = GetType<Data, "user.name">; // string
46+
* type PostsType = GetType<Data, "user.posts">; // { title: string; tags: string[] }[]
47+
* type PostType = GetType<Data, "user.posts[*]">; // { title: string; tags: string[] }
48+
* type TitleType = GetType<Data, "user.posts[*].title">; // string
49+
* type TagsType = GetType<Data, "user.posts[*].tags">; // string[]
50+
* type TagType = GetType<Data, "user.posts[*].tags[*]">; // string
2951
* ```
3052
*/
3153
export type GetType<T extends object, K extends DeepStrictObjectKeys<T>> =

src/types/IsUnion.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
import type { Equal } from './Equal';
22

33
/**
4-
* 제네릭 타입 `T`가 자기 자신의 요소와 동일한지를 검증한다.
4+
* Helper type that checks if a partition of type T is the same as the entire type T.
5+
* This works by distributing T through conditional types and checking if each partition
6+
* equals the whole. For union types, this produces `false | true`, while for single types
7+
* it produces just `false`.
8+
*
9+
* @internal
10+
* @template T - The type to check
11+
* @template P - The preserved original type (defaults to T)
512
*/
6-
type IsPartitionSameEntire<T, P = T> = T extends any // T를 각각의 요소 타입으로 분리하기 위한 조건부 타입
7-
? P extends T // 유니온 타입이면 이 조건부 타입에서 false | true가 나와야 한다.
13+
type IsPartitionSameEntire<T, P = T> = T extends any // Distribute T to each element for union types
14+
? P extends T // If T is a union, this conditional produces false | true
815
? false
916
: true
1017
: never;
1118

1219
/**
1320
* @title Type for checking if a type is a union type.
1421
*
15-
* This type uses the `IsPartitionSameEntire` type to check whether the provided type `T` is a union type.
16-
* - It works by partitioning the type `T` and checking if the type consists of multiple distinct elements.
17-
* - If the type `T` is a union type, the result will be `true`, otherwise `false`.
22+
* This type uses the `IsPartitionSameEntire` helper type to check whether the provided type `T` is a union type.
23+
* It works by partitioning the type `T` and checking if the type consists of multiple distinct elements.
24+
*
25+
* The detection mechanism:
26+
* - For union types: produces `boolean` (which is `false | true`)
27+
* - For single types: produces `false`
28+
* - Then checks if the result equals `boolean` to determine if it's a union
29+
*
30+
* @template T - The type to check
31+
* @returns `true` if T is a union type, `false` otherwise
1832
*
19-
* Example usage:
20-
* ```ts
33+
* @example
34+
* ```typescript
2135
* type Test1 = IsUnion<string | number>; // true
2236
* type Test2 = IsUnion<string>; // false
37+
* type Test3 = IsUnion<'a' | 'b' | 'c'>; // true
38+
* type Test4 = IsUnion<{ a: string } | { b: number }>; // true
39+
* type Test5 = IsUnion<never>; // false
40+
* type Test6 = IsUnion<unknown>; // false
41+
* type Test7 = IsUnion<any>; // false
42+
* type Test8 = IsUnion<boolean>; // true (boolean is true | false)
2343
* ```
2444
*/
2545
export type IsUnion<T> = Equal<IsPartitionSameEntire<T>, boolean> extends true ? true : false;

0 commit comments

Comments
 (0)