Use when defining global types. Use when augmenting window. Use when typing environment variables. Use when working with build-time constants. Use when configuring type definitions.
Your TypeScript environment includes globals, environment variables, and platform-specific APIs. Create accurate type definitions for your environment using declaration files (.d.ts). This ensures type safety for platform-specific code and global variables.
Model your environment accurately with .d.ts files. Declare globals, window properties, and environment variables that your code depends on.
// types/environment.d.ts
declare global {
interface Window {
APP_CONFIG: {
apiUrl: string;
version: string;
};
}
const BUILD_TIMESTAMP: number;
}
// Usage
console.log(window.APP_CONFIG.apiUrl);
console.log(BUILD_TIMESTAMP);