https://github.com/arundo/typed-env
Typed and validated environment variables made easy
https://github.com/arundo/typed-env
environment-variables types typescript validation zod
Last synced: 9 months ago
JSON representation
Typed and validated environment variables made easy
- Host: GitHub
- URL: https://github.com/arundo/typed-env
- Owner: arundo
- License: mit
- Created: 2023-11-28T08:56:13.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-10T09:15:28.000Z (about 2 years ago)
- Last Synced: 2025-06-16T17:05:30.594Z (10 months ago)
- Topics: environment-variables, types, typescript, validation, zod
- Language: TypeScript
- Homepage:
- Size: 101 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @arundo/typed-env
Typed environment variables using [Zod](https://zod.dev/).
## Installation
```sh
npm install @arundo/typed-env
# or
yarn add @arundo/typed-env
# or
pnpm add @arundo/typed-env
```
## Usage
Create a environment file with a schema and use `typeEnvironment` to create a typed environment object:
```ts
// environment.ts
import { z } from 'zod';
import { typeEnvironment } from '@arundo/typed-env';
export const envSchema = z.object({
NODE_ENV: z.enum(['test', 'development', 'production']),
PORT: z.coerce.number().int().default(3000),
});
export const environment = typeEnvironment(envSchema);
```
Import the environment object and use it:
```ts
// server.ts
import { environment } from './environment';
console.log(environment.NODE_ENV); // 'development' - type string
console.log(environment.PORT); // 3000 - type number
```
Set naming convention of environment variables:
```ts
// environment.ts
/* ... as usual ... */
export const environment = typeEnvironment(envSchema, { transform: 'camelcase' });
```
```ts
// server.ts
import { environment } from './environment';
console.log(environment.nodeEnv); // 'development' - type string
console.log(environment.port); // 3000 - type number
```
If you for some reason need to access the raw environment object, you can add types like this:
```ts
// environment.ts
import { z } from 'zod';
import { typeEnvironment } from '@arundo/typed-env';
export const envSchema = z.object({
PORT: z.coerce.number().int().default(3000),
});
export const environment = typeEnvironment(envSchema, { transform: 'camelcase' });
declare global {
namespace NodeJS {
interface ProcessEnv extends Record, string | undefined> {}
}
}
console.log(process.env.PORT); // '3000' - type string | undefined
```