A literal is asserted with as to the primitive type it already has
What it finds
"draft" as string, 0 as number, false as boolean: asserting a literal to the primitive type it already has changes nothing a reader or the compiler needs. It can even hide intent, since as const or a literal type is often what was meant.
Computed property names, as { ["--gap" as string]: 4 }, are left alone: there the assertion widens the key so that an object type with fixed keys accepts it.
The fix drops the assertion. It widens the type of the expression from the literal to the primitive as before, since TypeScript widens literals in the same places, and is offered as unsafe for the places where it does not.
From the rule's source, literal_widening_assertion.rs.
const a = "draft" as string;tx0 reports: this literal already has the type stringconst b = 0 as number;tx0 reports: this literal already has the type numberconst c = "draft" as const;const d = "x" as "x";const style = { ["--gap" as string]: 4 }; Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:literal-widening-assertion" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:literal-widening-assertion] reason="..."tx0 explain tx0/ts:literal-widening-assertion shows the same in the terminal. Found a false positive? Report it.