Skip to main content

isNonNull()

isNonNull<T>(value): value is T

Checks if a nullable value is not null.

Type guard that narrows Nullable<T> to T by excluding null.


Type Parametersโ€‹

T: Tโ€‹

The type of the value.


Parametersโ€‹

value: T | nullโ€‹

The nullable value to check.


Returns: value is Tโ€‹

true if the value is not null, false otherwise.


Sinceโ€‹

1.0.0


Exampleโ€‹

function process(data: `Nullable<string>`) {
if (isNonNull(data)) {
// data is now typed as string
console.log(data.toUpperCase());
}
}

How it works?โ€‹

Type guard that checks if a value is not null.

Type Narrowingโ€‹

Common Checksโ€‹

ValueResult
undefinedcheckmark true
0checkmark true
''checkmark true
falsecheckmark true
nullcross false

vs isNonNullableโ€‹


Use Casesโ€‹

Ensure value existence ๐Ÿ“Œโ€‹

Strictly check that a value is not null (allows undefined). Useful when null has a specific semantic meaning different from undefined.

if (isNonNull(value)) {
// value is T | undefined, but not null
}