Skip to content

isOptionalString

Jason Godesky edited this page Aug 15, 2026 · 1 revision

isOptionalString is a type guard that lets Typescript know that the variable passed in is either a string or undefined.

Parameters

candidate: unknown

Supply any variable to test if it’s either a string or undefined.

Returns

A type predicate for a string (candidate is string | undefined).

Examples

import { isOptionalString } from '@revolutionarygamesco/common'

isOptionalString('hello!') // true
isOptionalString(undefined) // true
isOptionalString(42) // false
isOptionalString(['hello!']) // false
isOptionalString({ a: 'hello!' }) // false

Clone this wiki locally