{"id":15007885,"url":"https://github.com/gervinfung/gen-env-type-def","last_synced_at":"2025-10-30T12:31:34.517Z","repository":{"id":171546366,"uuid":"648034436","full_name":"GervinFung/gen-env-type-def","owner":"GervinFung","description":"Generate type definitions for environment variables from different environment files with support for both import.meta.env and process.env","archived":false,"fork":false,"pushed_at":"2024-04-24T07:43:48.000Z","size":364,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-24T13:49:55.152Z","etag":null,"topics":["environment-variables","npm-package","type-definition","type-safety"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GervinFung.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-01T04:16:35.000Z","updated_at":"2024-05-30T08:13:04.731Z","dependencies_parsed_at":"2023-12-19T08:10:19.693Z","dependency_job_id":"5ef3bcd0-9b59-4a06-9c54-bfdd3d5b887a","html_url":"https://github.com/GervinFung/gen-env-type-def","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.040000000000000036","last_synced_commit":"ecdc73053f8a14482d65b90d76d4b2862d5e6347"},"previous_names":["gervinfung/gen-env-type-def"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GervinFung%2Fgen-env-type-def","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GervinFung%2Fgen-env-type-def/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GervinFung%2Fgen-env-type-def/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GervinFung%2Fgen-env-type-def/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GervinFung","download_url":"https://codeload.github.com/GervinFung/gen-env-type-def/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219856290,"owners_count":16556085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment-variables","npm-package","type-definition","type-safety"],"created_at":"2024-09-24T19:14:18.395Z","updated_at":"2025-10-30T12:31:34.512Z","avatar_url":"https://github.com/GervinFung.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gen-env-type-def\n\nA tool that generates types based on the value of each environment variable, using union type for enhanced type safety. This approach not only provides compile-time type checking but also guarantees runtime safety (unless there are dynamically generated environment variables that are not specified in `.env*` files)\n\n1. Generate union types based on different environment variables across multiple files.\n2. Support for both process.env and import.meta.env.\n\n# Feature\n\nSpecify the directory where the type definitions should be generated. Additionally, you can choose whether to generate types for `process.env` or `import.meta.env`.\n\nWith this tool, 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.\n\n# Usage\n\n#### API\n\nGiven root dir has arbitary set of environment variables in\n\n.env.development\n\n```\nNODE_ENV=development\nREQUIRED_IN_DEV_ONLY=\"true\"\nORIGIN=http://localhost:3000\nTIME_OUT=0\n```\n\n.env.production\n\n```\nNODE_ENV=production\nORIGIN=https://arkham.io\nTIME_OUT=2_000_000\n```\n\nand that of \u003croot_dir\u003e/backend in\n\n.env.development\n\n```\nDIR=backend-dev\nREPOSITORY_PROVIDER=github\n```\n\n.env.production\n\n```\nDIR=backend-pro\nREPOSITORY_PROVIDER=gitlab\n```\n\nUse `gen-env-type-def` like the following\n\n```ts\nimport { genEnvTypeDef } from 'gen-env-type-def'\n\ngenEnvTypeDef([{\n    inDir: __dirname // root dir,\n    envType: 'import.meta.env',\n    outDir: `${__dirname}/typing` // defaults to inDir\n}, {\n    inDir: `${__dirname}/backend` // backend,\n    envType: 'process.env',\n}])\n```\n\nWill generate `env.d.ts` in \u003croot_dir\u003e/backend and root dir\n\nin root dir, it's\n\n```js\n`${root_dir}/env.d.ts`;\n```\n\n```ts\ninterface ImportMetaEnv {\n\treadonly NODE_ENV: 'development' | 'production' | 'staging' | 'testing';\n\treadonly REQUIRED_IN_DEV_ONLY?: 'true';\n\treadonly ORIGIN: 'http://localhost:3000' | 'https://arkham.io';\n\treadonly TIME_OUT?: '0' | '2_000_000';\n}\ninterface ImportMeta {\n\treadonly env: ImportMetaEnv;\n}\n```\n\nbackend, it's\n\n```js\n`${root_dir}/backend/env.d.ts`;\n```\n\n```ts\ndeclare global {\n\tnamespace NodeJS {\n\t\tinterface ProcessEnv {\n\t\t\treadonly DIR: 'backend-dev' | 'backend-pro';\n\t\t\treadonly REPOSITORY_PROVIDER: 'github' | 'gitlab';\n\t\t}\n\t}\n}\n```\n\nAdditionally, you can also generate generic string type while keeping the type specified in environment files by doing so\n\n.env\n\n```\n// other environment variables\nRANDOM_ENV=yes\n```\n\n```ts\nimport { genEnvTypeDef } from 'gen-env-type-def';\n\ngenEnvTypeDef([\n\t{\n\t\tinDir: 'some-directory',\n\t\tenvType: 'import.meta.env',\n\t\tallowStringType: {\n\t\t\tfor: 'some', // all/some\n\t\t\tcase: 'include', // include/exclude listed variables\n\t\t\tvariables: ['RANDOM_ENV'],\n\t\t},\n\t},\n]);\n```\n\nWill generate the following output\n\n```ts\ninterface ImportMetaEnv {\n\t// other environment variables\n\tRANDOM_ENV: 'yes' | (string \u0026 {});\n}\ninterface ImportMeta {\n\treadonly env: ImportMetaEnv;\n}\n```\n\nThis is useful in scenarios where you have environment files like `.env.development`, `.env.test`, and `.env.production`, but you do not wish to commit `.env.production`. However, you have code that depends on this environment file, such as:\n\n```ts\nif (process.env.MODE === 'prodution') {\n\t// do something\n}\n```\n\nIn a CI/CD pipeline, this setup may fail depending on your ESLint or TypeScript configuration, as the only available types for MODE are `development` and `test`.\n\nAt 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\n\n# Contributions\n\nI 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.\n\nAdditionally, 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.\n\nYour 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgervinfung%2Fgen-env-type-def","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgervinfung%2Fgen-env-type-def","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgervinfung%2Fgen-env-type-def/lists"}