Skip to content
Sarj Code Standards

no-known-value-widening

Preserve a known value's contract instead of widening a local binding to unknown, object, or an unknown-valued dictionary.

Why

Erasing a typed value's fields forces downstream code to rediscover the same contract through casts, reflection, or representation checks.

Fix

Keep the inferred type, select the required domain fields, or use satisfies to check compatibility without erasing evidence.

Examples

Before — flagged A dictionary erases a known tool contract
src/tool.ts
type Tool = { name: string; enabled: boolean };
declare const tool: Tool;
const fields: Record<string, unknown> = tool;
After — preferred Retain the tool's fields
src/tool.ts
type Tool = { name: string; enabled: boolean };
declare const tool: Tool;
const fields = tool;
Before — flagged Retain the monomorphic factory contract — before
src/example.ts
declare function load(): { id: string };
const value: unknown = load();
After — preferred Retain the monomorphic factory contract — after
src/example.ts
declare function load(): { id: string };
const value = load();
Before — flagged Preserve a closed registry key set — before
src/example.ts
const handlers: Record<string, () => void> = { start() {} };
handlers.start();
After — preferred Preserve a closed registry key set — after
src/example.ts
type Command = "start";
const handlers = { start() {} } satisfies Record<Command, () => void>;
handlers.start();
Before — flagged Retain the independently known literal contract — before
src/example.ts
const value: unknown = { id: "u1" };
After — preferred Retain the independently known literal contract — after
src/example.ts
const value = { id: "u1" };
Before — flagged Retain a completely owned storage contract — before
src/example.ts
class Store {
readonly #value: unknown = { id: "u1" };
read() {
return this.#value;
}
}
After — preferred Retain a completely owned storage contract — after
src/example.ts
class Store {
readonly #value = { id: "u1" };
read() {
return this.#value;
}
}

References