Extending globalThis, window, process.env etc in TypeScript

2021-11-18

Based on TypeScript 4.5

Extending globalThis, global, window

Declaration:

declare var var_name: string

Use it:

globalThis.var_name
global.var_name
window.var_name
var_name

Extending process and process.env

Declaration:

declare namespace NodeJS {
interface Process {
some_key: string
}
interface ProcessEnv {
DATABASE_URL: string
}
}

Use it:

process.some_key
process.env.DATABASE_URL

Extending import.meta

Declaration:

declare interface ImportMeta {
env: {
DEV: boolean
}
}

Use it:

import.meta.env.DEV