Validated values
Define a field or result that must contain a concrete value after validation.
TypeScript · Type Alias
NonNullable<Type> removes null and undefined from a type. It is useful for describing values that remain after nullish cases have been ruled out.
NonNullable<Type> transforms a type by removing null and undefined from it. Applied to a union, it preserves the remaining members, making it useful for representing values after validation or other checks.
type NonNullable<Type> = Type & {};| Name | Type | Description |
|---|---|---|
Type | type | The input type from which null and undefined are excluded. |
A type derived from Type with null and undefined excluded.
type Present<T> = NonNullable<T>;
function requireValue<T>(value: T): NonNullable<T> {
if (value == null) throw new Error("Missing value");
return value as NonNullable<T>;
}
const name: string | undefined = "Ada";
const presentName = requireValue(name);
console.log(presentName.toUpperCase());This runnable example removes nullish possibilities from a generic result after a runtime check.
Define a field or result that must contain a concrete value after validation.
Derive a non-nullable type from an API or configuration union.
Express generic return types after rejecting nullish inputs.
ExcludeRequiredstrictNullChecksUtility Types