RevenueCat / RevenueCat/react-native-purchases

Vend a typeguard function for checking if an Error is a PurchasesError

Open
#291 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement triaged
Dominant language
TypeScript
Stars
1.2k
Forks
117
Avg merge
14h 40m
Merged PRs (30d)
40

Description

Hey. When using TypeScript, the type of the caught error is unknown (any in non-strict mode of older versions of TypeScript). It would be very useful to have a typeguard function vended that could narrow the type. Would you be willing to accept a PR that implements this?

export const isPurchasesError = (err: unknown): err is PurchasesError => {
  // Implementation here
}

The usage would look like:

try {
  await someCodeThatTriggersAPurchase()
} catch (err) {
  if (isPurchasesError(err)) {
    // Handle PurchasesError here. 
    // The type will be narrowed by the compiler and IDE completion will work nicely.
  } else {
    // Handle other errors differently
  }
}

This is something we can of course just write on our own end, but it seems like it'd be useful to have as part of the core library here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing PurchasesError type and the package's public TypeScript exports. Define the exported guard so it narrows unknown errors to PurchasesError, then verify that the example catch block receives the narrowed type and that non-PurchasesError values are rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
react-native, typescript
Domain
api, developer-experience
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.