type-assertion
an `as` cast in source code, where narrowing would prove the type instead
Deterministic checkblocking by defaultbasecode
The argument
Avoid as casts. They tell the compiler to trust you instead of proving the type is correct.
- Prefer type narrowing with
typeof,instanceof, or discriminated unions. - If an assertion is truly necessary (e.g., a library returns
unknown), add a brief comment explaining why narrowing is not possible. - Exception: test files may use
as unknown as Tto force invalid input into a defensive branch for coverage (see the unit-testing standards).
✅ GOOD: Narrowing
if (typeof value === 'string') {
return value.toUpperCase();
}
❌ BAD: Assertion without justification
return (value as string).toUpperCase();
The proof
failsrc/payloads/readLabel.ts
// The payload's field is `unknown`, and the cast says it is a string rather
// than proving it — a value from outside now travels as a checked one.
export const readLabel = ({ payload }: { payload: Record<string, unknown> }): string => (payload.label as string).toUpperCase();
pass
// `as const` is not the assertion this rule bans — it freezes literals, and the
// named-constants document asks for it by name.
export const PayloadKind = {
Label: 'label',
Amount: 'amount',
} as const;
export type PayloadKind = (typeof PayloadKind)[keyof typeof PayloadKind];
Turn it down
Both lines go in your lightsout.config.json.
"standards-checks": { "type-assertion": "advisory" }"standards-checks": { "type-assertion": "off" }