forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typings): create environment package to provide strict type chec…
…king for process.env (microsoft#20603) * feat(typings): create environment package to provide strict type checking for process.env * feat(tools): add new 'environment' global type to migration generator * fixup! feat(typings): create environment package to provide strict type checking for process.env * fixup! fixup! feat(typings): create environment package to provide strict type checking for process.env * fixup! feat(tools): add new 'environment' global type to migration generator * chore(typings): use solution config file to proerly check extensions/declaration without leaking globals * feat(typings): make environment globals work everywhere with proper DX
- Loading branch information
Showing
12 changed files
with
132 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": {}, | ||
"include": ["*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# environment | ||
|
||
Enables access to `process.env` in both node and browser packages (your code needs to rely on wepback or bundler that understands node environment). | ||
|
||
This definition list of env variables is maintained manually and should be extended as needed. | ||
|
||
## Usage | ||
|
||
```json | ||
{ | ||
"compilerOptions": { | ||
"types": ["environment"] | ||
} | ||
} | ||
``` | ||
|
||
Now you can use `process.env` global with strict type checking: | ||
|
||
```ts | ||
// @ExpectType string | ||
export function log(...messages: Array<string>) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.log(...messages); | ||
} | ||
|
||
// $ExpectError - 'prod' is not defined, did you mean to 'production' ? | ||
if (process.env.NODE_ENV === 'prod') { | ||
// do something | ||
} | ||
} | ||
``` | ||
|
||
## Adding env variables | ||
|
||
Add new env variables as needed | ||
|
||
**Example:** | ||
|
||
```diff | ||
// Adding NX_ENV env variable | ||
// ↓↓↓ | ||
interface ExtendedProcessEnv { | ||
NODE_ENV?: 'production' | 'development' | 'test'; | ||
LAGE_PACKAGE_NAME?: string; | ||
CI?: string; | ||
TF_BUILD?: string; | ||
+ NX_ENV?: string | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
declare namespace NodeJS { | ||
interface ExtendedProcessEnv { | ||
// env: { | ||
NODE_ENV?: 'production' | 'development' | 'test'; | ||
LAGE_PACKAGE_NAME?: string; | ||
CI?: string; | ||
TF_BUILD?: string; | ||
} | ||
|
||
/** | ||
* extending/creating ProcessEnv interface which is used in @types/node to define `process.env` | ||
* | ||
* 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.Process; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": {}, | ||
"include": ["*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": {}, | ||
"include": ["*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": {}, | ||
"include": ["*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": {}, | ||
"include": ["*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters