@amccarthy1/typed-env

中文文档
1.3.0发布于 2024/06/06MITTypeScriptESM + CJS零依赖近 30 天下载 9772

Typed environment variables

原始使用文档
文档目录4 节
  1. Get started
  2. Why is this useful
  3. What types are supported?
  4. What about optional variables?

Typed Env

codecov

A strongly-typed, 0-dependency environment variable parser for Typescript!

Get started

npm install --save @amccarthy1/typed-env  # npm
yarn add @amccarthy1/typed-env  # yarn
import { TypedEnv, EnumVar } from '@amccarthy1/typed-env'

const env = TypedEnv({
  ENVIRONMENT: EnumVar({ options: ['dev', 'staging', 'prod'] }),
})

doWhateverToRunApp(env.ENVIRONMENT) // env.ENVIRONMENT is of type 'dev' | 'staging' | 'prod'

Why is this useful

Many services use environment variables for runtime configuration, anything from the current environment or logging verbosity, to things like API keys and secrets. But many times, these environment variables are unvalidated and naively parsed from strings when needed.

This library aims to allow you to define environment variables in a more type-safe way. Take this example:

const environment = process.env.ENVIRONMENT

const makePayment = (amount: bigint) => {
  if (environment === 'prod') {
    makeRealPayment(amount)
  } else {
    makeMockPayment(amount)
  }
}

What if you configured your server with ENVIRONMENT=production instead of ENVIRONMENT=prod? Suddenly, all your users are getting free products because you're making mock payments instead of real ones!

If you'd used TypedEnv instead, you'd get this

const env = TypedEnv({
  ENVIRONMENT: EnumVar({ options: ['dev', 'staging', 'production'] }),
})

const makePayment = (amount: bigint) => {
  if (env.ENVIRONMENT === 'prod') {
    // TypeError!
    // `This condition will always return 'false' since
    // the types '"dev" | "staging" | "production"' and
    // '"prod"' have no overlap.`
    return makeRealPayment(amount)
  } else {
    return makeFakePayment(amount)
  }
}

What types are supported?

Currently, strings, enums, integers, booleans, and dates are supported, although you can define your own custom type using the Declaration<T> type

export type Declaration<T> = {
  variable?: string // The name of the environment variable; defaults to match the key if not specified
  parser: Parser<T> // A function (value: string) => T
}

What about optional variables?

TypedEnv is primarily built for required variables, but does support optionals using a defaultValue property. If you do not want to use an explicit default, you can use the optional() wrapper provided, which will automatically inject a default value of null and convert a Declaration<T> to a Declaration<T|null>

const env = TypedEnv({
  required: BoolVar({ defaultValue: false }),
  optional: optional(BoolVar()),
})
// This schema yields the following type object
const env: {
  required: boolean
  optional: boolean | null
}

It is generally preferred to use defaultValue over the optional wrapper, but optional may be useful in migrating existing codebases which already treat env vars as optional.

版本与发布记录

@amccarthy1/typed-env 共发布 8 个版本,首次发布于 2022/05/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.0latest2024/06/062 年前
1.2.02022/06/274 年前
1.1.12022/06/234 年前
1.1.02022/06/234 年前
1.0.22022/06/224 年前
1.0.12022/06/224 年前

关于 @amccarthy1/typed-env 的常见问题

@amccarthy1/typed-env 是什么?

Typed environment variables

如何安装 @amccarthy1/typed-env?

在项目目录运行 npm install @amccarthy1/typed-env,或使用 pnpm add / yarn add / bun add。

@amccarthy1/typed-env 支持 TypeScript 吗?

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

@amccarthy1/typed-env 支持 ESM 还是 CommonJS?

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

@amccarthy1/typed-env 可以商用吗?

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

@amccarthy1/typed-env 的下载量有多少?

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

@amccarthy1/typed-env 还在维护吗?

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