@chinmay02cs/typed-env
v1.0.3
Published
Type-safe environment variable validation for Node.js and TypeScript.
Downloads
68
Maintainers
Readme
typed-env
Type-safe environment variable validation for Node.js and TypeScript.
⚡ Why not dotenv?
dotenv only loads variables ❌
typed-env validates + types them ✅
🚀 Why typed-env?
Working with process.env is unsafe:
process.env.PORT; // string | undefined 😬This can cause:
- Missing variables ❌
- Wrong types ❌
- Runtime bugs ❌
typed-env validates and converts env variables at startup, giving you fully typed values.
📦 Installation
npm install @chinmay02cs/typed-env⚡ Usage
import { createEnv } from "@chinmay02cs/typed-env";
const env = createEnv({
PORT: "number",
NODE_ENV: ["development", "production"],
API_KEY: "string",
});
console.log(env.PORT); // number
console.log(env.NODE_ENV); // "development" | "production"✅ Example
.env
PORT=3000
NODE_ENV=development
API_KEY=abcd1234Code
const env = createEnv({
PORT: "number",
NODE_ENV: ["development", "production"],
API_KEY: "string",
});Result
{
PORT: 3000,
NODE_ENV: "development",
API_KEY: "abcd1234"
}🧠 Supported Types
1. String
API_KEY: "string";2. Number
PORT: "number";3. Enum (allowed values)
NODE_ENV: ["development", "production"];⚠️ Error Handling
Throws error if:
❌ Missing variable
Missing environment variable: PORT❌ Invalid number
PORT should be a number, got "abc"❌ Invalid enum value
NODE_ENV must be one of [development, production], got "staging"🔥 Features
- ✅ Type-safe env variables
- ✅ Runtime validation
- ✅ Enum support (via array)
- ✅ Zero dependencies
- ✅ Tiny and fast
📚 API
createEnv(schema)
createEnv({
KEY: "string" | "number" | string[]
})Returns a fully typed object based on your schema.
❌ Without typed-env
const port = process.env.PORT;
// string | undefined 😬✅ With typed-env
const env = createEnv({ PORT: "number" });
env.PORT; // number ✅🛠️ Future Improvements
- Boolean support
- Default values
- Optional variables
- Zod integration
