TypeScript · TS0009
Keep known evidence through local assertions
Detect immutable local values widened to a broad type and later asserted to a concrete type.
typescript/evidence/no-widen-then-assertsrc/rule-packs/typescript/rules/no-widen-then-assert.ts Upstream docs Run this rule
pnpm vite-doctor . --extends auto,typescript/strict --rules typescript/evidence/no-widen-then-assert
Why it matters
Widening a known value before asserting a new type separates the unsupported claim from its evidence. This rule follows immutable local aliases within one function; imported types and cross-function flows are outside its analysis.
Recommended fix
Preserve the original inferred type, or validate the value before claiming a different type.
Useful links
Example
Keep known evidence through local assertions
Before
const value = { id: 1 }; const erased: unknown = value; const user = erased as User
After
const value = { id: 1 }; const user = UserSchema.parse(value)