Skip to content

Instantly share code, notes, and snippets.

@dkundel
Created October 12, 2017 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkundel/c2e1dc80e7249054d4e89dcac597077c to your computer and use it in GitHub Desktop.
Save dkundel/c2e1dc80e7249054d4e89dcac597077c to your computer and use it in GitHub Desktop.
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