-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-type-safety.mdc
More file actions
66 lines (45 loc) · 2 KB
/
Copy pathconfig-type-safety.mdc
File metadata and controls
66 lines (45 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
description: Config and type safety rules - no defaults, no non-null assertions outside config
globs:
- "**/config/index.ts"
- "**/config/*.ts"
alwaysApply: false
---
# Config and Type Safety Rules
## Critical Requirements
### 1. Never Set Default Values in Config Files
**NEVER** set default values for environment variables in `config/index.ts` files. This includes:
- Empty strings: `process.env.VAR || ''`
- Fallback values: `process.env.VAR || 'default'`
- Nullish coalescing: `process.env.VAR ?? ''`
**Why**: Default values hide configuration errors and allow apps to start with invalid state.
### 2. Use Non-Null Assertions (`!`) in Config Files Only
Config files are the **one exception** where `!` assertions are allowed, because:
- All env vars must pass through startup validation before config is used
- Validation ensures required values exist before the app starts
- This keeps config files clean and typed as `string` (not `string | undefined`)
**Pattern for config files:**
```typescript
/* eslint-disable @typescript-eslint/no-non-null-assertion -- env vars validated at startup */
export const config = {
nodeEnv: process.env.NODE_ENV!,
apiPort: process.env.API_PORT!,
database: {
host: process.env.DB_HOST!,
},
};
```
### 3. Avoid Non-Null Assertions Elsewhere
Outside of config files, **avoid** `!` assertions. Prefer proper null checks, optional chaining, or type guards.
## Approved Patterns
### Backend Apps (api, management-api)
1. **Create startup validation** (`lib/startup/validation.ts`).
2. **Call validation early in app startup** before importing config.
3. **Use `!` in config with eslint-disable** (`config/index.ts`).
### Next.js Apps (web, management-web)
Validate at build time via app validate-env scripts and prebuild hooks where configured.
## Summary
| Location | `!` Allowed | Default Values |
| -------- | ----------- | ---------------- |
| `config/index.ts` | ✅ Yes (with eslint-disable) | ❌ Never |
| Other files | ❌ Avoid | Use at point of use if needed |