Skip to content

Commit

Permalink
feat(typings): make environment globals work everywhere with proper DX
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Nov 16, 2021
1 parent 17a6794 commit 6ebeb19
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions typings/environment/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
declare namespace NodeJS {
interface ExtendedProcessEnv {
env: {
NODE_ENV?: 'production' | 'development' | 'test';
LAGE_PACKAGE_NAME?: string;
CI?: string;
TF_BUILD?: string;
};
// env: {
NODE_ENV?: 'production' | 'development' | 'test';
LAGE_PACKAGE_NAME?: string;
CI?: string;
TF_BUILD?: string;
}

/**
* NOTE:
* proper DX and type-checking wont work whenever a library that uses `@types/webpack-env` is used in your code.
* extending/creating ProcessEnv interface which is used in @types/node to define `process.env`
*
* WHY?:
* - `@types/webpack-env` defines `env` as `any`, thus making extension incompatible. @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/webpack-env/index.d.ts#L275
* - `@storybook/addons` module type definitions internally reference `@types/webpack-env` so `process.env` type checking/DX wont work in all storybook configs/files
* NOTE:
* To make it work with and without node globals it need to use same token name
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v12/globals.d.ts#L764
*/
export interface ProcessEnv extends ExtendedProcessEnv {}

/**
* extending/creating `Process` interface which is used in @types/node to define `process` global
*
* NOTE:
* To make it work with and without node globals it need to use same token name
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v12/globals.d.ts#L883
*/
export interface Process {
env: ProcessEnv;
}
}

declare var process: NodeJS.ProcessEnv;
declare var process: NodeJS.Process;

0 comments on commit 6ebeb19

Please sign in to comment.