Maybe
Since 1.0.0
Add null/undefined to a type.
Signature
typescript
type Maybe<T> = T | null | undefinedParameters
| Parameter | Description |
|---|---|
T | The 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
}>Related
Nullable- Add null to a typeOptional- Add undefined to a typeNonNullable- Exclude null/undefined