Skip to content

What quick fixes keep ​

Every editor quick fix, safe-ts fix <rule> run, and safe-ts migrate codemod either preserves exact runtime behavior or states explicitly in its title what changes. Silently altering runtime behavior while staying compile-clean is treated as a bug—and guarded by an automated fix-meaning test suite.

In short

  • (the same as now): identical output for every input (undefined, null, "", 0, NaN, 0n, false).
  • Explicit behavior changes: titles name the exact edge case that shifts (e.g. Compare with 0 (NaN counts as true) or Use ?? (only for undefined)).
  • Type-only edits: annotation updates (unknown, Option<T>, Mut<T>) leave runtime execution untouched.

Two earlier bugs motivated this contract: in 0.2.0 null-to-undefined rewrote === null on external data into a permanently false === undefined, and in 0.4.0 a strict-truthiness fix produced unitId !== "" on an optional string (matching undefined). Both cases now run in CI across every quick fix and codemod.

safe-ts fix <rule> takes a fix marked (the same as now) by itself where a site has several and exactly one says so; any other fix it applies only where it is the site's only one or --fix picks it (Applying one rule's quick fixes).

How to read a title ​

  • "(the same as now)": every input gives the same result as before: undefined, null where the type has it, "", 0, NaN, 0n, false.
  • The title names what changes: Test for undefined only ("" counts as true), Compare with 0 (NaN counts as true), Use ?? (only for undefined), Compare with undefined (was always false), Wrap in unsafe(() => …): returns a Result you must check. Only those inputs, or only that part of the behaviour, change.
  • Types only: Replace any with unknown, Write as Option<...>, Declare that this function changes x: Mut<…>. Nothing runs differently.

NaN counts as false in a test (if (count)), and no comparison with 0 says so: the fixes for a number name it. A value read through a call (items.shift(), map.get(k)) is read once by the fix ((items.shift() ?? "") !== ""), since a second call can give another value.

Every quick fix ​

RuleFixWhat it does to the result
strict-truthinessTest for undefined and "" / 0n (the same as now)the same
Test for undefined and 0 (NaN counts as true)NaN now passes
Test for undefined only (… counts as true)"", 0, NaN, 0n now pass
Compare with "" / Compare with 0nthe same
Compare with 0 (NaN counts as true)NaN now passes
Test for a positive numbernegative numbers now fail
Spread the items that are keptthe same (with (NaN counts as true) when a number is tested)
compare-misses-undefinedLet undefined pass (the same as now), Test for "" only (the same as now)the same
Make undefined fail too, Treat undefined as ""undefined changes branch
prefer-is-over-tag, prefer-constructor-tagUse is(x, "Red"), Use Light.Redthe same; is(x, …) isn't offered on a value declared with null, which is doesn't take
exhaustive-switchAdd default: unreachable(…)the same for every variant
no-ts-enumConvert to a const object + union typethe same values; not offered for a numeric enum read as an object here (Color[0], Object.keys(Color))
no-null-check-for-undefinedCompare with undefined (the same as now)the same (== null already passed for undefined)
Compare with undefined (was always false / true), (the assertion always failed / passed), Use toBeUndefined() …the test now tests for undefined
no-undefined-check-for-nullCompare with null: … (the same as now)the same
Compare with null: … (was always false / true)the test now tests for null
prefer-nullish-defaultUse ?? (only for undefined), Use ??="", 0, NaN, false keep their value
prefer-q, needs-transformUse const x = q(...), Write it without q, Import …the same
must-use-resultDiscard explicitly with voidthe same
Propagate the error with q(…)the error is now returned
unhandled-failure, no-throw, number-parse-nan, floating-rejectionWrap in unsafe(…), Use the safe version, which returns a Result, Return err(…) instead of throwing, Add await, …the failure becomes a value, or is handled, as the title says
no-param-mutationUse toSorted(), Use toReversed(), Update a copythe same result; the caller's value is no longer changed. Update a copy only for a plain object's type declared in the file
readonly-droppedKeep it read-only: Readonly<…>the same: only the annotation changes; a write through it, which threw on the frozen value, is now refused by TypeScript
missing-mut, unneeded-mutLend it: mut(x), Pass it as it is: remove mut(…)the same: mut(x) is x (the transform erases it); only what TypeScript accepts changes
sort-comparatorSort numericallynumbers sort as numbers
dataview-endiannessBig-endian (false, the default) / Little-endian (true)the same / the other byte order
hoist-intlCreate it oncethe same
keep-error-cause, no-for-inKeep e as the cause, Iterate own keysthe error gets a cause; inherited keys are left out
no-type-predicate, brand-constructorLet TypeScript infer the narrowing from the bodythe same at runtime: only : x is T goes, and TypeScript infers the predicate from the body. Where the claim said more than the body tests, the code that relied on it stops compiling
no-ts-suppressionUse @ts-expect-errorthe same: a comment; TypeScript now reports it once the error is gone
no-method-signatureWrite handle as a readonly property with a function typethe same: only types; tsc then refuses an implementation whose parameter is narrower, and a reassignment of the member (drop readonly there)
no-hidden-anyWrite MessageEvent<unknown>, Replace Function with (...args: never[]) => unknown, Annotate event: MessageEvent<unknown>, Annotate value: unknownthe same: only types; tsc then reports each use that reads the value unchecked
unused-code, unused-privateDelete namenothing referred to it: what runs is the same. safe-ts fix unused-code <dirs> applies it to the whole program at once
unbound-methodBind it: this.tick.bind(this) (keeps this when it runs)the method runs on its object: what threw TypeError (this undefined) works
stale-across-awaitAwait first: const awaited = await …; then x += awaitedthe read moves after the await: a call that ran meanwhile is no longer lost (with one call at a time, the same)
unreleased-resourceRelease it in a finallythe same result; the release now runs on every way out of the block
unbalanced-updateUndo it in a finallychanges what the code does on purpose: the undo now runs on the way out that skipped it (the count is given back, the flag cleared); the result is the same otherwise

The Vue fixes (Add .value, Watch a getter, Use shallowRef, …) change what the component reads, as their titles say; they aren't run by the test below, which runs plain TypeScript.

How it is checked ​

crates/safe_ts_core/tests/meaning.rs writes small functions whose tested value is typed each way: T, T | undefined, x?: T, Option<T>, T | null, a field, a type from another file, a?.b, a call that changes state, and a function from another file, for strings, numbers, bigints and enums. It applies every fix the way safe-ts fix --fix "<title>" does, type-checks the code before and after, runs both in Node after the build-time transform over every value the type allows, and fails when a result changes where the title doesn't say so, or when the rule still reports the fixed code. Programs where a fix would change the result (splice to toSpliced, an array spread into an object) must not get it. A fix whose title isn't in its table fails the test too, also when it shows up in a fixture, and so does a title that says (the same as now) where the table doesn't say the result stays the same.

The codemods are checked the same way: each runnable case in tests/migrate/ has a probe.mjs, calls run on the code before and after the codemod. A thrown error and a returned Err of it count as the same failure; after null-to-undefined, a caller's null is passed as undefined, and data from JSON or the DOM keeps its null. The Vue cases, the package stand-ins (fake-store) and the enums merged with a namespace aren't run there: they are type-checked (tsc, vue-tsc) and compared with the expected output.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra