Skip to content

fix(message-builder): replace as-cast with explicit type annotation for defaultRules#837

Merged
kitsuyui merged 2 commits into
mainfrom
fix/remove-as-cast-from-defaultrules
May 27, 2026
Merged

fix(message-builder): replace as-cast with explicit type annotation for defaultRules#837
kitsuyui merged 2 commits into
mainfrom
fix/remove-as-cast-from-defaultrules

Conversation

@kitsuyui

Copy link
Copy Markdown
Owner

Summary

Replace the as type assertion on defaultRules with an explicit type annotation so TypeScript enforces exhaustiveness at compile time.

Problem

defaultRules was declared as:

const defaultRules = {
  pr_reaches_contain_only_one_nonzero_digit: true,
  // ...
} as { [key in RulesKey]: boolean }

The as assertion tells TypeScript to trust the object unconditionally. If a new key is added to Rules (and thus to RulesKey), omitting it from defaultRules produces no compile error. The missing key resolves to undefined at runtime, which is falsy, silently disabling the new rule in the default configuration without any warning.

Change

const defaultRules: { [key in RulesKey]: boolean } = {
  pr_reaches_contain_only_one_nonzero_digit: true,
  // ...
}

With the explicit annotation, TypeScript checks at compile time that every key in RulesKey has a corresponding entry in defaultRules. Adding a new rule to Rules without updating defaultRules now produces a type error.

No runtime behavior changes — the object contents are identical.

Trade-offs

  • The exhaustiveness check is now enforced by the type system rather than by convention, removing a silent failure mode.
  • Any future contributor who adds a rule to Rules must consciously decide whether it should be enabled by default, rather than having it silently default to disabled.

@github-actions

github-actions Bot commented May 26, 2026

Copy link
Copy Markdown

🎉 Happy commit!

@github-actions

github-actions Bot commented May 26, 2026

Copy link
Copy Markdown

gh-counter

PR gate

Removed Added +/-
TODO/FIXME 0 0 0

Repo dashboard

main (98984a1) #837 (42d508f) +/-
TODO/FIXME 0 0 0

Reported by gh-counter

@github-actions

github-actions Bot commented May 26, 2026

Copy link
Copy Markdown

Code Metrics Report

main (fd2ef70) #837 (42d508f) +/-
Coverage 99.1% 99.2% +0.0%
Code to Test Ratio 1:0.5 1:0.5 -0.1
Test Execution Time 1s 2s +1s
Details
  |                     | main (fd2ef70) | #837 (42d508f) |  +/-  |
  |---------------------|----------------|----------------|-------|
+ | Coverage            |          99.1% |          99.2% | +0.0% |
  |   Files             |              5 |              5 |     0 |
  |   Lines             |            122 |            131 |    +9 |
+ |   Covered           |            121 |            130 |    +9 |
- | Code to Test Ratio  |          1:0.5 |          1:0.5 |  -0.1 |
  |   Code              |           1598 |           1616 |   +18 |
+ |   Test              |            898 |            899 |    +1 |
- | Test Execution Time |             1s |             2s |   +1s |

Code coverage of files in pull request scope (96.0% → 96.0%)

Files Coverage +/- Status
src/message_builder.ts 96.0% 0.0% modified

Reported by octocov

…pe annotation change

Source map drift detected by test job's git diff --exit-code -- dist guard.
The defaultRules as-cast removal in src/message_builder.ts caused the source map
to be regenerated by tsdown but the new map was not included in the original
commit.
@github-actions

Copy link
Copy Markdown

gh-build-size

Target main #837 +/-
Total action artifacts 23,300,320 B 23,300,319 B -1 B
Source maps 17,130,662 B 17,130,661 B -1 B

Reported by gh-build-size

@kitsuyui
kitsuyui merged commit 8ee23ae into main May 27, 2026
5 checks passed
@kitsuyui
kitsuyui deleted the fix/remove-as-cast-from-defaultrules branch May 27, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant