Skip to main content

Overview

Constructs an Ok variant of Result. This represents a successful computation with a value.

Signature

function ok<T, E = never>(value: T): Ok<T, E>
function ok<T extends void = void, E = never>(value: void): Ok<void, E>

Parameters

value
T
required
The success value to wrap in an Ok result. Can be any type including null, undefined, or void.

Returns

Returns an Ok<T, E> instance containing the provided value.

Examples

Basic Usage

import { ok } from 'neverthrow'

const myResult = ok({ myData: 'test' })

myResult.isOk() // true
myResult.isErr() // false

With Null or Undefined

// With null
const okNull = ok(null)
okNull.isOk() // true
okNull._unsafeUnwrap() // null

// With undefined
const okUndefined = ok(undefined)
okUndefined.isOk() // true
okUndefined._unsafeUnwrap() // undefined

Type Annotations

// Explicitly specify both success and error types
const result = ok<number, string>(42)
// result is Ok<number, string>

// For void results
const voidResult = ok<void>(undefined)

Build docs developers (and LLMs) love