Skip to content

Optional

Since 1.0.0

Add undefined to a type.

Signature

typescript
type Optional<T> = T | undefined

Parameters

ParameterDescription
TThe base type

Description

Creates a type that can be undefined. Useful for representing optional values or values that may not be defined.

Examples

Basic Usage

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

type Name = Optional<string> // string | undefined

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

With Configuration

typescript
interface Config {
  apiKey: string
  timeout: Optional<number>
  retries: Optional<number>
}

const config: Config = {
  apiKey: 'abc123',
  // timeout and retries can be omitted
}

Released under the MIT License.