@expo/nullthrows

中文文档
2.1.0发布于 2025/08/03MITTypeScriptESM + CJS零依赖近 30 天下载 5390

An idiomatic way to enforce values not to be null nor undefined, with first-class support for TypeScript and ES modules

原始使用文档
文档目录4 节
  1. Installation and usage
  2. API
  3. function nullthrows<T>(value: T | null | undefined, message…
  4. History

nullthrows

An idiomatic way to enforce values not to be null or undefined, with first-class support for TypeScript. Very lightweight with no dependencies.

Installation and usage

npm install @expo/nullthrows
import process from 'node:process';
import nullthrows from '@expo/nullthrows';

// nodeEnv is guaranteed to be a string. If the environment variable is
// undefined, nullthrows throws a TypeError.
const nodeEnv = nullthrows(process.env.NODE_ENV);

// You can optionally provide a custom error message
const nodeEnv2 = nullthrows(process.env.NODE_ENV, `NODE_ENV must be defined`);

API

function nullthrows<T>(value: T | null | undefined, message?: string): NonNullable<T>

Parameters

  • value: the value to enforce not to be null or undefined
  • message: an optional error message to use in the thrown TypeError if the given value is null or undefined

Type parameters

  • T: the static type of value

Return value

Returns value if it is neither null nor undefined.

Throws

Throws a TypeError if value is null or undefined.

History

This package is a spiritual successor of nullthrows. The motivation for a new package was primarily to support using TypeScript with ES modules since the type declarations included with nullthrows produce type errors.

版本与发布记录

@expo/nullthrows 共发布 3 个版本,首次发布于 2022/07/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2025/08/031.3 年前
2.0.02025/08/011.3 年前
1.0.02022/07/144 年前

关于 @expo/nullthrows 的常见问题

@expo/nullthrows 是什么?

An idiomatic way to enforce values not to be null nor undefined, with first-class support for TypeScript and ES modules

如何安装 @expo/nullthrows?

在项目目录运行 npm install @expo/nullthrows,或使用 pnpm add / yarn add / bun add。

@expo/nullthrows 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@expo/nullthrows 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@expo/nullthrows 可以商用吗?

@expo/nullthrows 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@expo/nullthrows 的下载量有多少?

根据 npm 官方下载统计,@expo/nullthrows 在 2026/10/14 – 2026/11/12 这 30 天内被下载 5,390 次,日均约 180 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@expo/nullthrows 还在维护吗?

最新版本 2.1.0 发布于 2025/08/03(1.3 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/07/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。