Skip to content

isDefined

Category
Export Size
115 B
Last Changed
5 days ago

Non-nullish checking type guard for React ref objects and plain values

Demo

Usage

tsx
import { 
isDefined
} from '@reause/shared'
import {
useRef
} from 'react'
const
example
=
useRef
(
Math
.
random
() ? 'example' :
undefined
) // RefObject<string | undefined>
if (
isDefined
(
example
))
example
.
current
// string — narrowed by the type guard

Type Declarations

ts
export type IsDefinedReturn = boolean

export function isDefined<T>(v: RefObject<T>): v is RefObject<Exclude<T, null | undefined>>

export function isDefined<T>(v: T): v is Exclude<T, null | undefined>

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
f273a - fix(shared): resolve isDefined audit findings (#700)
7e3e1 - feat(shared): add isDefined (#27)

Released under the MIT License. v0.1.8