env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

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

Open ivawzh opened this issue 4 years ago • 2 comments

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 =]

ivawzh avatar Jul 01 '20 15:07 ivawzh

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.

toddbluhm avatar Jul 01 '20 23:07 toddbluhm

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 {}
  }
}

jlalmes avatar Apr 13 '23 06:04 jlalmes