Skip to content

itslukej/typed-env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typed-env

npm version monthly downloads

A typed environment variable parser with support for choices, custom parsers, and more.

Usage

import { createEnv } from 'typed-env';

const env = createEnv({
  PORT: { type: 'number', default: 80 }
});

env.PORT; // number

Features

  • Strongly typed
  • Supports custom parsers
  • Supports optional environment variables
  • Supports limiting the possible values (see Choices)
  • Supports passing custom environments (see Options)

Choices

import { createEnv } from 'typed-env';

const env = createEnv({
  NODE_ENV: {
    type: 'string',
    choices: ['development', 'production']
  }
});

env.NODE_ENV; // 'development' | 'production'

Parser

You can pass a parser function to return your own custom type

import { createEnv } from 'typed-env';

const env = createEnv({
  HOMEPAGE: { parser: url => new URL(url) }
});

env.HOMEPAGE; // URL

Options

If you want to use a custom env, pass env in the options parameter, otherwise it will load from process.env

interface Options {
  env?: Record<string, string> | NodeJS.ProcessEnv;
}

About

💾 Typed environment variable parser.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 5