Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Support TypeScript file, e.g. env.ts #151

Open
ivawzh opened this issue Jul 1, 2020 · 2 comments · May be fixed by #399
Open

[Feature Request] Support TypeScript file, e.g. env.ts #151

ivawzh opened this issue Jul 1, 2020 · 2 comments · May be fixed by #399

Comments

@ivawzh
Copy link

ivawzh commented Jul 1, 2020

Hi, thanks for maintaining this awesome library.

Would you please make support of TypeScript env file? E.g.

// staging.env.ts

const staging: ENV = { }
export default staging

A significant benefit would be type-check support across env files.

Thanks in advanced =]

@toddbluhm
Copy link
Owner

toddbluhm commented Jul 1, 2020

Hello and thank you for the feature request.

Unfortunately, I have no plans to support Typescript files. The main reason is that in order to support typescript files, I would have to depend on the typescript library (probably ts-node as well) in order to compile the typescript files so that they can be read. The typescript library is very large and quite unnecessary for most of the users of this library.

Perhaps someday things will be different in regards to typescript, but for now, including such a large library (when env-cmd is trying to be as dependency-free as possible) is just not something I desire for this project.

However, as I have stated before in the past, if enough users request this feature, I am happy to reconsider adding it.

@jlalmes
Copy link

jlalmes commented Apr 13, 2023

This should work 👇

// env-vars.d.ts
import envCmdRc from './.env-cmdrc.json';
type ENV_CMD_RC = typeof envCmdRc;

type Merge<A, B> = {
  [K in keyof A | keyof B]: K extends keyof A
    ? K extends keyof B
      ? string
      : string | undefined
    : K extends keyof B
    ? string | undefined
    : never;
};

type MergeEnvs<T extends object[]> = T extends [infer A, ...infer Rest]
  ? A extends object
    ? Rest extends object[]
      ? Merge<A, MergeEnvs<Rest>>
      : never
    : never
  : {};

type LOCAL_ENV_VARS = ENV_CMD_RC['local'];
type DEVELOPMENT_ENV_VARS = ENV_CMD_RC['development'];
type STAGING_ENV_VARS = ENV_CMD_RC['staging'];
type PRODUCTION_ENV_VARS = ENV_CMD_RC['production'];

type ENV_VARS = MergeEnvs<
  [LOCAL_ENV_VARS, DEVELOPMENT_ENV_VARS, STAGING_ENV_VARS, PRODUCTION_ENV_VARS]
>;

declare global {
  namespace NodeJS {
    interface ProcessEnv extends ENV_VARS {}
  }
}

@k-yle k-yle linked a pull request Jan 10, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants