Skip to content

Conversation

@yasaichi
Copy link

@yasaichi yasaichi commented Aug 10, 2025

This PR implements support for passing this context to the generator function in safeTry, following the established pattern already implemented in similar libraries like Effect and typescript-result.

Closes #632

Usage

class MyClass {
  private multiplier = 2;
  
  getValue(): number {
    return 10;
  }
  
  myMethod(): Result<number, string> {
    return safeTry(this, function* () {
      const value = this.getValue();
      return ok(value * this.multiplier);
    });
  }
}

Why This Matters

This feature enhances the ergonomics of safeTry when used in class-based designs, eliminating the need for manual binding to preserve context (e.g. that). This brings neverthrow in line with other modern Result/Effect libraries that have already adopted this pattern.

yasaichi and others added 4 commits August 10, 2025 22:03
- Add overloads for safeTry to accept `this` context as first parameter
- Support both sync and async generators with proper type inference
- Maintain backward compatibility with existing API
- Implement GitHub issue supermacro#632

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add tests for sync/async generators with this context
- Tests mirror existing test structure for consistency
- All 121 tests passing

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add example usage of safeTry with this context in class methods
- Use async generator example demonstrating this binding
- Use type inference approach for cleaner code examples

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Remove explicit type parameters from examples to rely on type inference
- Fix async generator return type from Promise<Result> to ResultAsync

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Aug 10, 2025

⚠️ No Changeset found

Latest commit: 5fe739d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Allow Passing this Context to safeTry

2 participants