Always validate configuration objects using proper schema validation (like Zod) before type casting or using the configuration values. Avoid unsafe type casting from input types to output types without validation, as this can lead to runtime errors when expected properties are undefined.
Configuration resolution should follow a clear hierarchy: file-based config โ inline config โ defaults. Parse and validate configurations at the appropriate boundaries, not after unsafe casting.
Example of the problem:
// Risky - casting before validation
let userConfig = options as Config
// userConfig.routesDirectory may not be defined yet
// Better approach
const validatedConfig = configSchema.parse({
...fileConfig,
...inlineConfig,
...defaults
})
When building configuration utilities, accept optional directory parameters to avoid hardcoded paths, and leverage existing config resolution functions rather than duplicating logic. This ensures consistent behavior across different entry points and prevents configuration drift.
Enter the URL of a public GitHub repository