Result type representing either a success with data or a failure with error. This discriminated union enables type-safe error handling without try/catch blocks.
The type of the successful data
The type of the error, defaults to ErrorWithStatus
const result: Result<string> = await tryCatch(fetchText());if (result.error) { // TypeScript knows result.data is null here console.error(result.error.message);} else { // TypeScript knows result.error is null here console.log(result.data.toUpperCase());} Copy
const result: Result<string> = await tryCatch(fetchText());if (result.error) { // TypeScript knows result.data is null here console.error(result.error.message);} else { // TypeScript knows result.error is null here console.log(result.data.toUpperCase());}
Result type representing either a success with data or a failure with error. This discriminated union enables type-safe error handling without try/catch blocks.