feat(core): add flatTapErr and isResult#40
Merged
Conversation
flatTapErr is the error-channel mirror of flatTap: it runs a
Result-returning effect on the error, keeps the original error when the
effect succeeds, and threads the effect's error otherwise
(Result<T, E | E2>). A throw becomes a Defect like every other
combinator. Use it for a failable effect during error handling (e.g.
writing the error to an audit log that may itself fail). Added on both
Result and AsyncResult.
isResult(x) is a standalone guard narrowing an unknown to
Result<unknown, unknown> (and Result.isResult). It checks the Result
prototype, so a { tag: "Ok" } look-alike is not matched and an
AsyncResult returns false. For untyped interop boundaries.
Closes the only structural gaps found comparing the surface to boxed /
neverthrow / byethrow; everything else they have (error accumulation,
Option, generator do-notation, asyncMap) stays deliberately excluded.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR closes two missing surface-area gaps in @unthrown/core by adding an error-channel “failable tap” combinator and a safe unknown-to-Result type guard, aligning the library’s Result API with peer libraries while preserving unthrown’s defect/throw invariants.
Changes:
- Add
flatTapErrto bothResultandAsyncResultto run aResult/AsyncResult-returning effect onErr, preserving the original error on effect success and threading effect failures. - Add
isResult(x)(andResult.isResult) implemented as a prototype/instanceofcheck to safely narrow fromunknown. - Add/extend runtime tests, type-level tests, docs, and a changeset entry for the new API.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/types.ts | Adds flatTapErr to the public ResultMethods and AsyncResult type surfaces. |
| packages/core/src/types.test-d.ts | Adds type-level assertions for flatTapErr error widening and isResult narrowing. |
| packages/core/src/core.ts | Implements Res.flatTapErr, AsyncRes.flatTapErr, and exports isResult. |
| packages/core/src/index.ts | Re-exports isResult from the package entrypoint. |
| packages/core/src/facade.ts | Exposes Result.isResult on the facade object and updates docs link list. |
| packages/core/src/result.spec.ts | Adds behavioral tests for Result.flatTapErr (success keeps original error; failures thread). |
| packages/core/src/async-result.spec.ts | Adds behavioral tests for AsyncResult.flatTapErr (sync/async effects, passthrough behavior). |
| packages/core/src/invariants.spec.ts | Extends invariants coverage (throw→Defect, Defect passthrough) to include flatTapErr. |
| packages/core/src/facade.spec.ts | Verifies facade wiring for Result.isResult and basic behavior. |
| packages/core/src/constructors.spec.ts | Adds runtime tests for isResult true/false cases including look-alikes and AsyncResult. |
| docs/guide/core-concepts.md | Documents flatTapErr in the error channel list and introduces isResult for unknown. |
| docs/guide/choosing-a-combinator.md | Adds flatTapErr to the combinator selection table and guidance section. |
| CLAUDE.md | Updates the internal “surface”/invariants documentation to include flatTapErr and isResult. |
| .changeset/flat-tap-err-is-result.md | Declares a minor release and summarizes the added API. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Closes the only two structural gaps surfaced by comparing unthrown's surface against boxed, neverthrow, and byethrow. (The functions that comparison initially flagged as "missing" —
flatMapError,mapOkToResult,mapErrorToResult— already exist under standard names:orElseandflatMap.)flatTapErr— failable tap on the error channelThe error-side mirror of
flatTap. Runs aResult-returning effect on the error; on the effect'sOkthe original error flows through, otherwise the effect's error is threaded (Result<T, E | E2>). A throw becomes aDefectlike every other combinator. Added on bothResultandAsyncResult.Use it for a failable effect during error handling — e.g. writing the error to an audit log that may itself fail. (byethrow ships this as
orThrough; unthrown previously had only the puretapErr, an asymmetry vs.tap/flatTapon the success side.)isResult(x)— guard fromunknownA standalone type guard (and
Result.isResult) narrowing anunknowntoResult<unknown, unknown>, for untyped interop boundaries. It checks the value carries theResultprototype, so a plain{ tag: "Ok" }look-alike is not matched, and anAsyncResultreturnsfalse. (boxed and byethrow both ship anisResult.)Deliberately still excluded
Everything else the peers have conflicts with a stated thesis or the "small / done-able" goal: error accumulation (
combineWithAllErrors/collect), Option, generator do-notation (safeTry), and neverthrow'sasyncMap/asyncAndThen(which would bypass boundary qualification, Thesis #3).Coverage
Full gate green: format · lint · typecheck (15/15, incl. type-level tests for
flatTapErrchannel widening andisResultnarrowing) · knip · test (165 tests, functions/lines 100%) · build. New behavior is guarded 1:1 ininvariants.spec.ts(throw→Defect, Defect-passthrough) plusresult/async-result/constructors/facadespecs.🤖 Generated with Claude Code