Skip to content

typescript.global_mutable_state

Correctness High

Detects module-level mutable state.

Global mutable state causes subtle bugs:

  • Request leakage - In serverless, state persists between invocations
  • Race conditions - Concurrent requests modify shared state
  • Testing pain - State leaks between tests
  • Debugging difficulty - State changes from anywhere
// ❌ Before
let cache: Map<string, User> = new Map();
let requestCount = 0;
export function getUser(id: string) {
requestCount++;
if (!cache.has(id)) {
cache.set(id, fetchUser(id));
}
return cache.get(id);
}
// ✅ After
// Encapsulate in class
class UserService {
private cache = new Map<string, User>();
getUser(id: string) {
if (!this.cache.has(id)) {
this.cache.set(id, this.fetchUser(id));
}
return this.cache.get(id);
}
}
// Or use dependency injection
export function createUserService(cache: Cache) {
return {
getUser(id: string) {
return cache.get(id) ?? fetchUser(id);
}
};
}
  • let at module level with mutable values
  • Module-level arrays, objects, Maps, Sets
  • Global variables that get reassigned

Unfault suggests encapsulating state or using const.