Skip to content

Maybe

Since 1.0.0

Add null/undefined to a type.

Signature

typescript
type Maybe<T> = T | null | undefined

Parameters

ParameterDescription
TThe base type

Description

Creates a type that can be null or undefined. This is a combination of Nullable and Optional.

Examples

Basic Usage

typescript
import type { Maybe } from 'uni-types'

type Name = Maybe<string> // string | null | undefined

function greet(name: Maybe<string>) {
  if (!name) {
    return 'Hello, stranger!'
  }
  return `Hello, ${name}!`
}

With API Responses

typescript
interface ApiResponse<T> {
  data: Maybe<T>
  error: Maybe<string>
  loading: boolean
}

type UserResponse = ApiResponse<{
  id: number
  name: string
}>

Released under the MIT License.