feat(types): add es-toolkit/types module with compile-time type utilities#1818
Draft
dayongkr wants to merge 9 commits into
Draft
feat(types): add es-toolkit/types module with compile-time type utilities#1818dayongkr wants to merge 9 commits into
dayongkr wants to merge 9 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…ties Introduce a declaration-only `es-toolkit/types` entrypoint for compile-time type utilities. v1 ships seven everyday helpers that TypeScript does not provide natively: ValueOf, Simplify, Merge, SetOptional, SetRequired, Writable, and NonEmptyArray. The module is packaged as declaration-only: publishConfig exposes it through the `types` condition (no runtime `default`), and postbuild removes the empty JS the build emits, so consumers receive only .d.ts/.d.mts. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
attw fails under node10 resolution because it ignores the "exports" map and the types module has no root shim. Create a declaration-only root shim (types.d.ts without a runtime types.js) in postbuild, matching how the other modules are exposed to node10 consumers. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
9b73758 to
b8d7033
Compare
Recursive Partial for nested object patches (config overrides, mocks). Arrays recurse into elements without becoming sparse, tuples keep their arity, and functions/Date/RegExp pass through unchanged. Single type parameter, no options. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Drop the Map/Set handling: patch-shaped types are JSON-like in practice, and the extra branches doubled the type for a tail case. The chain now reads pass-through -> array -> object -> primitive. Support can be added back non-breakingly if requested. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Recursive Readonly for immutable state and no-mutation function parameters. Same skeleton as DeepPartial: arrays/tuples become readonly recursively, functions/Date/RegExp pass through. Map/Set traversal intentionally omitted — sampled real-world usage showed 0/23 resolvable Deep* arguments contain them. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Without these branches DeepReadonly leaves Map/Set mutable (.set/.add survive)
and DeepPartial lets a plain {} pass as a Map while demanding complete values
inside — both break the utility's promise. DeepReadonly folds Map into the
ReadonlyMap check (structural), DeepPartial keeps the mutable/readonly split.
Chains are formatted flat, one case per line, for readability.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
merge/toMerged recurse into plain objects and arrays only; Map and Set are replaced wholesale. Recursing into their contents at the type level let an incomplete Map replace a complete one through merge, producing values that lie about their type. Map/Set (and their readonly variants) now pass through, so they must be provided complete — exactly what merge does with them. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Settle on one broad structural boundary for the deep utilities: containers (objects, arrays/tuples, Map/Set) recurse, atomic built-ins (functions, Date, RegExp) pass through — the same traversal cloneDeep performs and the same boundary DeepReadonly uses. The merge caveat (Map/Set are replaced wholesale at runtime) is documented on the type instead of narrowing it. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
One it() per intent. Drop asserts already implied by whole-shape equality
("leaves other keys unchanged"), representation checks (NonEmptyArray's tuple
form), and duplicate happy paths. Deep* specs already followed this rule.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
975c501 to
591a46c
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds a declaration-only
es-toolkit/typesentrypoint for compile-time type utilities. v1 ships nine everyday helpers TypeScript lacks natively:ValueOf<T>keyof)Simplify<T>Merge<T, U>Uoverrides overlapping keysSetOptional<T, K>Partial)SetRequired<T, K>Required)Writable<T>readonlyfrom all properties (inverse ofReadonly)NonEmptyArray<T>[T, ...T[]])DeepPartial<T>Partialfor nested object patches; plain objects, arrays/tuples (non-sparse), andMap/Setcontents recurse;Date/RegExp/functions pass throughDeepReadonly<T>Readonlyfor immutable state; arrays/tuples become readonly,Map/SetbecomeReadonlyMap/ReadonlySetSelection rules
"Standard" = TypeScript native types (
lib.*.d.ts).Partial,Omit,NonNullable,Awaited,Uppercase, …).Except,StrictOmit,Exactskipped). Only fill what's genuinely missing.ValueOfmirrorskeyof).Packaging
Declaration-only:
publishConfig.exportsexposes./typesvia thetypescondition (no runtimedefault), andpostbuildstrips the empty JS — ships only.d.ts/.d.mts. Use withimport type.Deep utilities: where the special-casing stops
Deep utilities recurse into data containers (plain objects, arrays/tuples,
Map/Set) and pass through atomic built-ins that appear in real data shapes (functions,Date,RegExp). Nothing else is special-cased (WeakMap, typed arrays,Blob, DOM types, class instances fall through to the object branch): observed demand for them is ~0, the fallout is mild, and adding a case later is non-breaking.Tests
Co-located
*.spec.tswith VitestexpectTypeOf(existing convention). 9 files, 22 tests, passing under--typecheck. Specs assert intended design decisions only (including negative cases via@ts-expect-error, e.g. sparse arrays stay illegal underDeepPartial).Remaining TODO
JsonValuefamily →UnionToIntersection→LiteralUnion→Brand→DeepRequiredNote: the
isNonEmptyguard (pairs withNonEmptyArray) belongs inpredicate— separate PR.