TypeScript environment variable decorator
A perfect TypeScript environment variables library.
- Strongly-typed declarative class containing your environment data
- Supports both static and instance properties
- Type-casting using TypeScript metadata reflection
- Auto UPPER_SNAKE_CASE conversion
- Converts environment values "FALSE", "false", "0" to false for boolean types
- Throws runtime error if variable doesn't exist
- Supports default values
- Makes decorated properties read-only in runtime
- ❤️ You will like it
💼 Use cases
🪞 Type-casting Using TypeScript metadata reflection
Just specify class field type and classenv
will cast the environment variable string value to the value of your field type.
Only string
, number
, and boolean
is supported.
process.env['PORT'] = '3000';
class ServerSettings {
@Env('PORT')
portNumber!: number;
@Env('PORT')
portString!: string;
@Env('PORT')
portBoolean!: boolean;
}
🐍 Auto UPPER_SNAKE_CASE from camelCase conversion
No need to manually specify the environment variable name
process.env['POSTGRES_URL'] = 'postgres://127.0.0.1:5432';
class PostgresAdapter {
@Env()
postgresUrl!: string;
}
🫙 Use default value in case of environment variable absence
class ServerSettings {
@Env()
port: number = 3000;
}
🚔 Throw runtime error if no value provided
One could say "It's a bad practice to throw runtime error"
, and it's a right assertion, but not in this case.
Most of the time your application can't work without all the environment variables.
You don't want to run application in an indefinite state and then debug these strange things.
So classenv
will throw runtime error and your application should shut down with an informative message of what's going wrong.
class PostgresAdapter {
@Env()
postgresUrl!: string;
}
But in case the environment variable is not required – you can just assign a default value for the field, and it will not throw.
class PostgresAdapter {
@Env()
postgresUrl: string = 'postgres://127.0.0.1:5432';
}
🔘 Pick one of the names from array
process.env['POSTGRES_URL'] = 'postgres://127.0.0.1:5432';
class PostgresAdapter {
@Env(['POSTGRESQL_URI', 'PG_URL', 'POSTGRES_URL'])
url!: string;
}
✨ static
field also supported
process.env['PORT'] = '3000';
class ServerSettings {
@Env()
static port: number;
}
1️⃣ Boolean type casting 0️⃣
If value is 0
of false
in any case (FaLsE
also included, since it's .toLowerCase()
'd under the hood) – it becomes false
.
Otherwise - true
process.env['FALSE'] = 'false';
process.env['ZERO'] = '0';
process.env['TRUE'] = 'true';
process.env['ANYTHING'] = 'Jast a random string';
class Common {
@Env()
static FALSE!: boolean;
@Env()
static zero!: boolean;
@Env()
static TRUE!: boolean;
@Env()
static anything!: boolean;
}
🛑 @Env()
decorated properties are read-only in runtime
Environment is something established from outside, so you definitely should not modify it in your application.
process.env['PORT'] = '3000';
class ServerSettings {
@Env()
static port!: number;
}
ServerSettings.port = 5000;
❗Dependencies❗
It is important, classenv
can not work without it.
reflect-metadata
npm i reflect-metadata
And then import it somewhere close to your entry point (index.ts
/main.ts
/etc...).
Should be imported before any of your environment classes.
import 'reflect-metadata';
tsconfig.json
These settings should be enabled
"emitDecoratorMetadata": true,
"experimentalDecorators": true,