accurate-environment-model
Create an Accurate Model of Your Environment
Overview
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.
When to Use This Skill
- Defining global types
- Augmenting window or globalThis
- Typing environment variables
- Working with build-time constants
- Configuring type definitions
The Iron Rule
Model your environment accurately with .d.ts files. Declare globals, window properties, and environment variables that your code depends on.
Example
// 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);
Reference
- Effective TypeScript, 2nd Edition by Dan Vanderkam
- Item 76: Create an Accurate Model of Your Environment
More from marius-townhouse/effective-typescript-skills
exclusive-or-properties
Use when exactly one of several properties should be present. Use when modeling mutually exclusive options. Use when building component props with alternative configurations. Use when designing API parameters that have variants.
11module-by-module-migration
Use when migrating large codebases. Use when converting JavaScript to TypeScript. Use when managing dependencies. Use when planning migration order. Use when teams are adopting TypeScript.
11editor-interrogation
Use when debugging type inference. Use when types behave unexpectedly. Use when learning unfamiliar code.
11allowjs-mixing
Use when migrating JavaScript to TypeScript. Use when gradually adopting TypeScript. Use when working with mixed codebases. Use when converting large projects. Use when teams are learning TypeScript.
11codegen-over-complex-types
Use when types become extremely complex. Use when types mirror external schemas. Use when maintaining type-to-schema mappings. Use when types require extensive type-level logic. Use when types drift from data sources.
10limit-optional-properties
Use when adding optional properties. Use when types have many optional fields. Use when considering required vs optional.
10