Skip to content

Function: match()

Call Signature

match<R, OkReturn, ErrReturn>(onOk, onErr): (result) => OkReturn | ErrReturn

Defined in: fp.ts:132

Takes handlers for Ok and Err results and returns a function that accepts a Result. The returned function pattern matches on the Result, calling the appropriate handler and returning its result.

Type Parameters

R

R extends Result

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

onOk

(data) => NonPromise<OkReturn>

Function to handle Ok results

onErr

(error) => NonPromise<ErrReturn>

Function to handle Err results

Returns

A function that takes a Result object and returns the result of the appropriate handler

(result): OkReturn | ErrReturn

Parameters

result

NonPromise<R>

Returns

OkReturn | ErrReturn

Example

typescript
const result1 = pipe(ok("hello"), match(
  (data: string) => `Success: ${data}`,
  (error: string) => `Error: ${error}`
)); // "Success: hello"

const result2 = pipe(err("failed"), match(
  (data: string) => `Success: ${data}`,
  (error: string) => `Error: ${error}`
)); // "Error: failed"

Call Signature

match<R, OkReturn, ErrReturn>(onOk, onErr): (result) => Promise<OkReturn | ErrReturn>

Defined in: fp.ts:136

Takes handlers for Ok and Err results and returns a function that accepts a Result. The returned function pattern matches on the Result, calling the appropriate handler and returning its result.

Type Parameters

R

R extends Result

The input Result type

OkReturn

OkReturn

The type returned by the success handler

ErrReturn

ErrReturn

The type returned by the error handler

Parameters

onOk

(data) => OrPromise<OkReturn>

Function to handle Ok results

onErr

(error) => OrPromise<ErrReturn>

Function to handle Err results

Returns

A function that takes a Result object and returns the result of the appropriate handler

(result): Promise<OkReturn | ErrReturn>

Parameters

result

OrPromise<R>

Returns

Promise<OkReturn | ErrReturn>

Example

typescript
const result1 = pipe(ok("hello"), match(
  (data: string) => `Success: ${data}`,
  (error: string) => `Error: ${error}`
)); // "Success: hello"

const result2 = pipe(err("failed"), match(
  (data: string) => `Success: ${data}`,
  (error: string) => `Error: ${error}`
)); // "Error: failed"