npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gen-env-type-def

v0.0.4

Published

Generate type definitions for process.env or import.meta.env

Downloads

23

Readme

gen-env-type-def

Initially, I found myself type guarding environment variables either as process.env.SOMETHING or import.meta.env.SOMETHING, treating them as generic strings. However, I realized that I could go beyond just basic type safety and ensure the correct import of environment variables in production. Instead of writing individual type guards, I decided to create a function that generates specific types for each environment variable, using discriminated unions for enhanced type safety. This approach not only provides compile-time type checking but also guarantees runtime safety.

But that's not all. I encountered a different way of importing environment variables while using Vite. Instead of using process.env.SOMETHING, Vite uses import.meta.env.SOMETHING, requiring a different type definition.

During my search for tools that handle this task, I came across "gen-env-types" by Ben Awad, which does a great job. However, I found it lacking in generating discriminated union types based on different environment variables across multiple files and limited to only process.env. I desired a tool that could fulfill both requirements.

That's why I developed this tool – to provide the following capabilities:

  1. Generate discriminated union types based on different environment variables across multiple files.
  2. Support for both process.env and import.meta.env.

With this tool, you can ensure type safety, handle various environment variables, and streamline your development process.

Feature

The gen-env-type-def tool is designed to generate type definitions based on different .env* files. To use it, simply specify the directory where the type definitions should be generated. Additionally, you can choose whether to generate types for process.env or import.meta.env.

With gen-env-type-def, you have the flexibility to provide different input configurations for each directory, allowing you to generate specific type definitions for various environments or configurations within your project.

By leveraging this tool, you can effortlessly generate precise and thorough type definitions for your environment variables. This enhances type safety and enables smooth integration with your development workflow, ensuring a seamless and reliable environment for your project.

Usage

API

Given root dir has arbitary set of environment variables in

.env.development

NODE_ENV=development
REQUIRED_IN_DEV_ONLY="true"
ORIGIN=http://localhost:3000
TIME_OUT=0

.env.production

NODE_ENV=production
ORIGIN=https://arkham.io
TIME_OUT=2_000_000

and that of <root_dir>/backend in

.env.development

DIR=backend-dev
REPOSITORY_PROVIDER=github

.env.production

DIR=backend-pro
REPOSITORY_PROVIDER=gitlab

Use gen-env-type-def like the following

import { genEnvTypeDef } from 'gen-env-type-def'

genEnvTypeDef([{
    inDir: __dirname // root dir,
    envType: 'import.meta.env',
    outDir: `${__dirname}/typing` // defaults to inDir
}, {
    inDir: `${__dirname}/backend` // backend,
    envType: 'process.env',
}])

Will generate env.d.ts in <root_dir>/backend and root dir

in root dir, it's

`${root_dir}/env.d.ts`;
interface ImportMetaEnv {
	readonly NODE_ENV: 'development' | 'production' | 'staging' | 'testing';
	readonly REQUIRED_IN_DEV_ONLY?: 'true';
	readonly ORIGIN: 'http://localhost:3000' | 'https://arkham.io';
	readonly TIME_OUT?: '0' | '2_000_000';
}
interface ImportMeta {
	readonly env: ImportMetaEnv;
}

backend, it's

`${root_dir}/backend/env.d.ts`;
declare global {
	namespace NodeJS {
		interface ProcessEnv {
			readonly DIR: 'backend-dev' | 'backend-pro';
			readonly REPOSITORY_PROVIDER: 'github' | 'gitlab';
		}
	}
}

At this stage, I trust that I have effectively conveyed the benefits and capabilities of using gen-env-type-def to generate type definitions for environment variables. This tool empowers developers to ensure type safety and seamless integration with their development workflow by automatically generating accurate and comprehensive type definitions based on environment variable configurations

Contributions

I appreciate your active participation in the development process. If you come across any bugs, have feature requests, or need clarification on any aspect of the project, please don't hesitate to open an issue.

Additionally, your contributions to the project are highly valued. If you have ideas or improvements that you would like to implement, I invite you to suggest a pull request. Simply fork the repository, make the necessary code changes, add relevant tests to ensure quality, and push your changes.

Your feedback and contributions play an essential role in making the project better, and I am grateful for your involvement. Thank you for your support and participation in the development of the project.