try-error Documentation
Installation
Get started with try-error in your TypeScript project
Package Installation
Installation
$pnpm add try-error
Note: try-error has zero runtime dependencies and works with TypeScript 4.5+
TypeScript Configuration
For the best experience, ensure your tsconfig.json
has strict mode enabled:
tsconfig.jsonjson
{
"compilerOptions": {
"strict": true,
"exactOptionalPropertyTypes": true,
"noUncheckedIndexedAccess": true
}
}
Basic Usage
Import the functions you need and start handling errors safely:
Basic Exampletypescript
1import { trySync, tryAsync, isTryError } from 'try-error';
2
3// Synchronous operations
4const result = trySync(() => JSON.parse(jsonString));
5if (isTryError(result)) {
6 console.error('Parse failed:', result.message);
7} else {
8 console.log('Parsed data:', result);
9}
10
11// Asynchronous operations
12const asyncResult = await tryAsync(() => fetch('/api/data'));
13if (isTryError(asyncResult)) {
14 console.error('Fetch failed:', asyncResult.message);
15} else {
16 const data = await asyncResult.json();
17}