es-toolkit 1.51.0-dev.2080 → 1.51.0-dev.2082

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ //#region src/types/EmptyObject.d.ts
2
+ /**
3
+ * An object with no properties.
4
+ *
5
+ * Pairs with the `isEmptyObject` guard. Assigning an object that has any property
6
+ * fails, because every value would have to be `never`.
7
+ *
8
+ * @example
9
+ * const a: EmptyObject = {}; // ok
10
+ * const b: EmptyObject = { a: 1 }; // error
11
+ *
12
+ * @example
13
+ * // Useful for a step that carries no data.
14
+ * interface StepContext {
15
+ * intro: EmptyObject;
16
+ * form: { amount: number };
17
+ * }
18
+ */
19
+ type EmptyObject = Record<PropertyKey, never>;
20
+ //#endregion
21
+ export { EmptyObject };
@@ -0,0 +1,21 @@
1
+ //#region src/types/EmptyObject.d.ts
2
+ /**
3
+ * An object with no properties.
4
+ *
5
+ * Pairs with the `isEmptyObject` guard. Assigning an object that has any property
6
+ * fails, because every value would have to be `never`.
7
+ *
8
+ * @example
9
+ * const a: EmptyObject = {}; // ok
10
+ * const b: EmptyObject = { a: 1 }; // error
11
+ *
12
+ * @example
13
+ * // Useful for a step that carries no data.
14
+ * interface StepContext {
15
+ * intro: EmptyObject;
16
+ * form: { amount: number };
17
+ * }
18
+ */
19
+ type EmptyObject = Record<PropertyKey, never>;
20
+ //#endregion
21
+ export { EmptyObject };
@@ -0,0 +1,18 @@
1
+ //#region src/types/IsEqual.d.ts
2
+ /**
3
+ * Resolves to `true` when `A` and `B` are exactly the same type, `false` otherwise.
4
+ *
5
+ * Unlike a plain conditional type, this tells `any` apart from every other type,
6
+ * which makes it useful for catching an accidental `any` in type-level tests.
7
+ *
8
+ * @template A - The first type to compare.
9
+ * @template B - The second type to compare.
10
+ *
11
+ * @example
12
+ * type A = IsEqual<{ a: string }, { a: string }>; // true
13
+ * type B = IsEqual<string, 'literal'>; // false
14
+ * type C = IsEqual<unknown, any>; // false
15
+ */
16
+ type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false;
17
+ //#endregion
18
+ export { IsEqual };
@@ -0,0 +1,18 @@
1
+ //#region src/types/IsEqual.d.ts
2
+ /**
3
+ * Resolves to `true` when `A` and `B` are exactly the same type, `false` otherwise.
4
+ *
5
+ * Unlike a plain conditional type, this tells `any` apart from every other type,
6
+ * which makes it useful for catching an accidental `any` in type-level tests.
7
+ *
8
+ * @template A - The first type to compare.
9
+ * @template B - The second type to compare.
10
+ *
11
+ * @example
12
+ * type A = IsEqual<{ a: string }, { a: string }>; // true
13
+ * type B = IsEqual<string, 'literal'>; // false
14
+ * type C = IsEqual<unknown, any>; // false
15
+ */
16
+ type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false;
17
+ //#endregion
18
+ export { IsEqual };
@@ -0,0 +1,24 @@
1
+ //#region src/types/JsonValue.d.ts
2
+ /**
3
+ * Any value that `JSON.parse` can produce.
4
+ *
5
+ * Functions, `Date`, `undefined`, and class instances are excluded, because they
6
+ * do not survive a JSON round trip.
7
+ *
8
+ * @example
9
+ * declare function parse(text: string): JsonValue;
10
+ *
11
+ * const value = parse('{"a":[1,null]}');
12
+ * if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
13
+ * const a = value.a; // JsonValue
14
+ * }
15
+ *
16
+ * @example
17
+ * // Use `Record` when you want to accept a JSON object specifically.
18
+ * declare function send(body: Record<string, JsonValue>): void;
19
+ */
20
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
21
+ [key: string]: JsonValue;
22
+ };
23
+ //#endregion
24
+ export { JsonValue };
@@ -0,0 +1,24 @@
1
+ //#region src/types/JsonValue.d.ts
2
+ /**
3
+ * Any value that `JSON.parse` can produce.
4
+ *
5
+ * Functions, `Date`, `undefined`, and class instances are excluded, because they
6
+ * do not survive a JSON round trip.
7
+ *
8
+ * @example
9
+ * declare function parse(text: string): JsonValue;
10
+ *
11
+ * const value = parse('{"a":[1,null]}');
12
+ * if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
13
+ * const a = value.a; // JsonValue
14
+ * }
15
+ *
16
+ * @example
17
+ * // Use `Record` when you want to accept a JSON object specifically.
18
+ * declare function send(body: Record<string, JsonValue>): void;
19
+ */
20
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
21
+ [key: string]: JsonValue;
22
+ };
23
+ //#endregion
24
+ export { JsonValue };
@@ -0,0 +1,14 @@
1
+ //#region src/types/Primitive.d.ts
2
+ /**
3
+ * Every primitive value in JavaScript. Anything that is not a primitive is an object.
4
+ *
5
+ * Writing this union by hand usually misses `bigint` or `symbol`.
6
+ *
7
+ * @example
8
+ * function isPrimitive(value: unknown): value is Primitive {
9
+ * return value === null || (typeof value !== 'object' && typeof value !== 'function');
10
+ * }
11
+ */
12
+ type Primitive = string | number | bigint | boolean | symbol | null | undefined;
13
+ //#endregion
14
+ export { Primitive };
@@ -0,0 +1,14 @@
1
+ //#region src/types/Primitive.d.ts
2
+ /**
3
+ * Every primitive value in JavaScript. Anything that is not a primitive is an object.
4
+ *
5
+ * Writing this union by hand usually misses `bigint` or `symbol`.
6
+ *
7
+ * @example
8
+ * function isPrimitive(value: unknown): value is Primitive {
9
+ * return value === null || (typeof value !== 'object' && typeof value !== 'function');
10
+ * }
11
+ */
12
+ type Primitive = string | number | bigint | boolean | symbol | null | undefined;
13
+ //#endregion
14
+ export { Primitive };
@@ -0,0 +1,20 @@
1
+ import { Simplify } from "./Simplify.mjs";
2
+
3
+ //#region src/types/SetOptional.d.ts
4
+ /**
5
+ * Makes the given keys `K` of `T` optional, leaving the rest unchanged.
6
+ * Like the built-in `Partial`, but scoped to specific keys.
7
+ *
8
+ * Distributes over unions, so a union stays a union.
9
+ *
10
+ * @template T - The object type to transform.
11
+ * @template K - The keys to make optional.
12
+ *
13
+ * @example
14
+ * type User = { id: number; name: string; email: string };
15
+ * type UserDraft = SetOptional<User, 'email'>;
16
+ * // => { id: number; name: string; email?: string }
17
+ */
18
+ type SetOptional<T, K extends keyof T> = T extends unknown ? Simplify<Omit<T, K> & Partial<Pick<T, K>>> : never;
19
+ //#endregion
20
+ export { SetOptional };
@@ -0,0 +1,20 @@
1
+ import { Simplify } from "./Simplify.js";
2
+
3
+ //#region src/types/SetOptional.d.ts
4
+ /**
5
+ * Makes the given keys `K` of `T` optional, leaving the rest unchanged.
6
+ * Like the built-in `Partial`, but scoped to specific keys.
7
+ *
8
+ * Distributes over unions, so a union stays a union.
9
+ *
10
+ * @template T - The object type to transform.
11
+ * @template K - The keys to make optional.
12
+ *
13
+ * @example
14
+ * type User = { id: number; name: string; email: string };
15
+ * type UserDraft = SetOptional<User, 'email'>;
16
+ * // => { id: number; name: string; email?: string }
17
+ */
18
+ type SetOptional<T, K extends keyof T> = T extends unknown ? Simplify<Omit<T, K> & Partial<Pick<T, K>>> : never;
19
+ //#endregion
20
+ export { SetOptional };
@@ -0,0 +1,20 @@
1
+ import { Simplify } from "./Simplify.mjs";
2
+
3
+ //#region src/types/SetRequired.d.ts
4
+ /**
5
+ * Makes the given keys `K` of `T` required, leaving the rest unchanged.
6
+ * Like the built-in `Required`, but scoped to specific keys.
7
+ *
8
+ * Distributes over unions, so a union stays a union.
9
+ *
10
+ * @template T - The object type to transform.
11
+ * @template K - The keys to make required.
12
+ *
13
+ * @example
14
+ * type User = { id: number; name: string; avatar?: string };
15
+ * type ProfileUser = SetRequired<User, 'avatar'>;
16
+ * // => { id: number; name: string; avatar: string }
17
+ */
18
+ type SetRequired<T, K extends keyof T> = T extends unknown ? Simplify<T & Required<Pick<T, K>>> : never;
19
+ //#endregion
20
+ export { SetRequired };
@@ -0,0 +1,20 @@
1
+ import { Simplify } from "./Simplify.js";
2
+
3
+ //#region src/types/SetRequired.d.ts
4
+ /**
5
+ * Makes the given keys `K` of `T` required, leaving the rest unchanged.
6
+ * Like the built-in `Required`, but scoped to specific keys.
7
+ *
8
+ * Distributes over unions, so a union stays a union.
9
+ *
10
+ * @template T - The object type to transform.
11
+ * @template K - The keys to make required.
12
+ *
13
+ * @example
14
+ * type User = { id: number; name: string; avatar?: string };
15
+ * type ProfileUser = SetRequired<User, 'avatar'>;
16
+ * // => { id: number; name: string; avatar: string }
17
+ */
18
+ type SetRequired<T, K extends keyof T> = T extends unknown ? Simplify<T & Required<Pick<T, K>>> : never;
19
+ //#endregion
20
+ export { SetRequired };
@@ -0,0 +1,24 @@
1
+ //#region src/types/UnknownRecord.d.ts
2
+ /**
3
+ * An object with unknown keys and unknown values.
4
+ *
5
+ * Use it instead of `{}`, which accepts every non-nullish value including numbers
6
+ * and strings. Values are `unknown`, so reading one forces a check first.
7
+ *
8
+ * Only types with an index signature are assignable. An `interface` declares its
9
+ * keys one by one and is rejected, so accept `object` when the caller may pass
10
+ * one, or spread it at the call site.
11
+ *
12
+ * @example
13
+ * function log(data: UnknownRecord) {
14
+ * if (typeof data.id === 'string') {
15
+ * console.log(data.id);
16
+ * }
17
+ * }
18
+ *
19
+ * log({ id: '1' }); // ok
20
+ * log(42); // error, while `{}` would have allowed it
21
+ */
22
+ type UnknownRecord = Record<PropertyKey, unknown>;
23
+ //#endregion
24
+ export { UnknownRecord };
@@ -0,0 +1,24 @@
1
+ //#region src/types/UnknownRecord.d.ts
2
+ /**
3
+ * An object with unknown keys and unknown values.
4
+ *
5
+ * Use it instead of `{}`, which accepts every non-nullish value including numbers
6
+ * and strings. Values are `unknown`, so reading one forces a check first.
7
+ *
8
+ * Only types with an index signature are assignable. An `interface` declares its
9
+ * keys one by one and is rejected, so accept `object` when the caller may pass
10
+ * one, or spread it at the call site.
11
+ *
12
+ * @example
13
+ * function log(data: UnknownRecord) {
14
+ * if (typeof data.id === 'string') {
15
+ * console.log(data.id);
16
+ * }
17
+ * }
18
+ *
19
+ * log({ id: '1' }); // ok
20
+ * log(42); // error, while `{}` would have allowed it
21
+ */
22
+ type UnknownRecord = Record<PropertyKey, unknown>;
23
+ //#endregion
24
+ export { UnknownRecord };
@@ -8,7 +8,14 @@ import { ToPascalCaseKeys } from "./ToPascalCaseKeys.mjs";
8
8
  import { ToSnakeCaseKeys } from "./ToSnakeCaseKeys.mjs";
9
9
  import { DeepPartial } from "./DeepPartial.mjs";
10
10
  import { DeepReadonly } from "./DeepReadonly.mjs";
11
+ import { EmptyObject } from "./EmptyObject.mjs";
12
+ import { IsEqual } from "./IsEqual.mjs";
13
+ import { JsonValue } from "./JsonValue.mjs";
11
14
  import { NonEmptyArray } from "./NonEmptyArray.mjs";
15
+ import { Primitive } from "./Primitive.mjs";
16
+ import { SetOptional } from "./SetOptional.mjs";
17
+ import { SetRequired } from "./SetRequired.mjs";
18
+ import { UnknownRecord } from "./UnknownRecord.mjs";
12
19
  import { ValueOf } from "./ValueOf.mjs";
13
20
  import { Writable } from "./Writable.mjs";
14
- export { type DeepPartial, type DeepReadonly, type Merge, type NonEmptyArray, type ObjectKeys, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type ValueOf, type Writable };
21
+ export { type DeepPartial, type DeepReadonly, type EmptyObject, type IsEqual, type JsonValue, type Merge, type NonEmptyArray, type ObjectKeys, type Primitive, type SetOptional, type SetRequired, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type UnknownRecord, type ValueOf, type Writable };
@@ -8,7 +8,14 @@ import { ToPascalCaseKeys } from "./ToPascalCaseKeys.js";
8
8
  import { ToSnakeCaseKeys } from "./ToSnakeCaseKeys.js";
9
9
  import { DeepPartial } from "./DeepPartial.js";
10
10
  import { DeepReadonly } from "./DeepReadonly.js";
11
+ import { EmptyObject } from "./EmptyObject.js";
12
+ import { IsEqual } from "./IsEqual.js";
13
+ import { JsonValue } from "./JsonValue.js";
11
14
  import { NonEmptyArray } from "./NonEmptyArray.js";
15
+ import { Primitive } from "./Primitive.js";
16
+ import { SetOptional } from "./SetOptional.js";
17
+ import { SetRequired } from "./SetRequired.js";
18
+ import { UnknownRecord } from "./UnknownRecord.js";
12
19
  import { ValueOf } from "./ValueOf.js";
13
20
  import { Writable } from "./Writable.js";
14
- export { type DeepPartial, type DeepReadonly, type Merge, type NonEmptyArray, type ObjectKeys, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type ValueOf, type Writable };
21
+ export { type DeepPartial, type DeepReadonly, type EmptyObject, type IsEqual, type JsonValue, type Merge, type NonEmptyArray, type ObjectKeys, type Primitive, type SetOptional, type SetRequired, type Simplify, type ToCamelCaseKeys, type ToConstantCaseKeys, type ToKebabCaseKeys, type ToPascalCaseKeys, type ToSnakeCaseKeys, type UnknownRecord, type ValueOf, type Writable };
@@ -38,6 +38,6 @@
38
38
  * });
39
39
  * ```
40
40
  */
41
- declare function attempt<T, E>(func: () => T): [null, T] | [E, null];
41
+ declare function attempt<T, E = unknown>(func: () => T): [null, T] | [E, null];
42
42
  //#endregion
43
43
  export { attempt };
@@ -38,6 +38,6 @@
38
38
  * });
39
39
  * ```
40
40
  */
41
- declare function attempt<T, E>(func: () => T): [null, T] | [E, null];
41
+ declare function attempt<T, E = unknown>(func: () => T): [null, T] | [E, null];
42
42
  //#endregion
43
43
  export { attempt };
@@ -31,6 +31,6 @@
31
31
  * });
32
32
  * // users is typed as User[]
33
33
  */
34
- declare function attemptAsync<T, E>(func: () => Promise<T>): Promise<[null, T] | [E, null]>;
34
+ declare function attemptAsync<T, E = unknown>(func: () => Promise<T>): Promise<[null, T] | [E, null]>;
35
35
  //#endregion
36
36
  export { attemptAsync };
@@ -31,6 +31,6 @@
31
31
  * });
32
32
  * // users is typed as User[]
33
33
  */
34
- declare function attemptAsync<T, E>(func: () => Promise<T>): Promise<[null, T] | [E, null]>;
34
+ declare function attemptAsync<T, E = unknown>(func: () => Promise<T>): Promise<[null, T] | [E, null]>;
35
35
  //#endregion
36
36
  export { attemptAsync };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.51.0-dev.2080+f4e08794",
3
+ "version": "1.51.0-dev.2082+0949c8b5",
4
4
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",