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โ
| Value | Result |
|---|---|
undefined | true |
0 | true |
'' | true |
false | true |
null | 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
}
true
false