@sentinelagent/ts-env
v1.4.2
Published
Type-safe environment variable loading for TypeScript. Zero dependencies.
Downloads
70
Maintainers
Readme
ts-env
Type-safe environment variable loading for TypeScript. Zero dependencies.
Install
npm install ts-envUsage
import { strictEnv } from 'ts-env';
const env = strictEnv({
DATABASE_URL: { type: 'url', description: 'PostgreSQL connection string' },
PORT: { type: 'port', default: 3000 },
DEBUG: { type: 'boolean', default: false },
API_KEY: { type: 'string' },
});
// env.DATABASE_URL → string | env.PORT → number | env.DEBUG → booleanThrows on startup if required vars are missing or invalid. No silent failures.
API
| Function | Description |
|----------|-------------|
| strictEnv(schema, source?) | Validates env, throws with all errors if invalid |
| loadEnv(schema, source?) | Returns { env, errors, ok } — no throw |
Field types
| Type | Output | Validates |
|------|--------|-----------|
| string | string | trimmed |
| number | number | isNaN check |
| boolean | boolean | true/false/1/0/yes/no |
| url | string | new URL() |
| port | number | 1–65535 |
License
MIT © trysentinelagent
