Skip to Content
Octopus Energy Packages

Generate types

Get type-safe access to environment variables with varlock codegen

Use this pattern to generate a TypeScript declaration file from your schema, so process.env access is fully typed.

Update the schema

Add the @generateTsTypes decorator to your .env.schema header:

.env.schema
# @generateTsTypes(path=types/env.d.ts)

The path is relative to the schema file. Varlock writes the declaration file to that location.

Add a script

Add a generate:env script to your package.json:

package.json
{
  "scripts": {
    "generate:env": "varlock codegen"
  }
}

Run it to generate (or regenerate) the types:

npm run generate:env

The generated file augments process.env with the variables defined in your schema. Commit it to the repository so editors pick up the types without running the generator.

On this page