Created
October 12, 2017 12:30
-
-
Save dkundel/c2e1dc80e7249054d4e89dcac597077c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type Safe<T> = { [P in keyof T]: string }; | |
function getSafeEnvironment<T extends {}>(e: T): Safe<T> { | |
return new Proxy(e, { | |
get: function(env: any, key: string): string { | |
const value: any = env[key]; | |
if (value == null) { | |
throw new Error("'" + key + "' environment variable is not set."); | |
} | |
return value; | |
} | |
}); | |
} | |
process.env.FOO | |
const safeEnv = getSafeEnvironment(process.env); | |
safeEnv.FOO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment