lightsoutAlpha

type-assertion

Telling the compiler a type instead of proving it.

Deterministic checkBlocks by default

The rule

Agents read this text as written when they write and review code.

Never write an as cast in source code. A cast tells the compiler to trust you instead of proving the type. Narrow instead, with typeof, instanceof or a discriminated union. When a value cannot be narrowed where it is used, such as a value of type unknown from a library, write a type guard or a validation function for it.

This includes text arriving at a boundary, such as a JSON payload, a query parameter or a database value: a string is not yet a member of a named constant's union, so convert it with a small validation function, such as parseAction, never as Action.

A type guard must test the value. One that returns true without checking is a cast under another name.

as const is not a cast: it fixes a literal's type, and it stays.

// Incorrect: the compiler takes your word for it
return (value as string).toUpperCase();

// Correct: the check proves it
if (typeof value === 'string') {
	return value.toUpperCase();
}

Examples

The check flags the incorrect code and passes the correct code. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.

Incorrect

srcpayloads
src/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();

Correct

srcpayloadscommonconstants
src/payloads/common/constants/PayloadKind.ts
// `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];

Configure

  • Block"blocking"Stops a run when a file the run changed breaks the rule.
    Default
  • Advise"advisory"Reports it and hands it to the refactor agent. Never stops a run.
  • Off"off"Not checked. Use it when your own linter already enforces the rule.

Add this to your lightsout.config.json, then change the value.

lightsout.config.json
{
"standards-rule-settings": {
"type-assertion": "blocking"
}
}

Packs that select it