Skip to content

fix: improve type-safe - #330

Merged
kazupon merged 17 commits into
mainfrom
fix/type-safe-experience
Oct 4, 2025
Merged

kazupon merged 17 commits into
mainfrom
fix/type-safe-experience

Conversation

@kazupon

@kazupon kazupon commented Sep 30, 2025

Copy link
Copy Markdown
Owner

Description

Linked Issues

Additional context

Summary by CodeRabbit

  • New Features

    • Added defineWithTypes and lazyWithTypes for stronger, explicit typing of commands and plugins.
    • Exposed CommandRunner and improved CLI typing to support richer typed command runners and args/extensions.
  • Documentation

    • Major Type System guide overhaul with detailed examples for typed define/lazy variants and plugin extensions.
    • Updated lazy/async guide (public config export, clearer terminology); README and guides wording fixes and link updates.
  • Chores

    • Updated docs dev alias to debug@4.4.3.
  • Misc

    • Example updated to remove a suppressed type-check, surfacing compile-time validation.

@coderabbitai

coderabbitai Bot commented Sep 30, 2025

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

Walkthrough

Adds typed helpers defineWithTypes and lazyWithTypes, expands Gunshi param/args/extensions typing and CommandContext constraints, updates exports/tests/docs to use the new APIs, and makes smaller doc/config/example adjustments. Changes are type-level and test/docs updates; runtime behavior is unchanged.

Changes

Cohort / File(s) Summary
Core API & Types
packages/gunshi/src/definition.ts, packages/gunshi/src/types.ts, packages/gunshi/src/index.ts, packages/gunshi/src/context.ts
Adds defineWithTypes and lazyWithTypes; updates define/lazy typings and overloads; expands GunshiParamsConstraint/ExtractArgs; refines LazyCommand/Commandable; updates exports to include new helpers.
Core Tests & Test Utils
packages/gunshi/src/definition.test.ts, packages/gunshi/src/plugin/core.test.ts, packages/gunshi/test/utils.ts
Tests updated to exercise typed helpers; replaces define uses with defineWithTypes where appropriate; adds DeepWriteable test helper; adjusts assertions for args/extensions and runner contexts.
Definition Package (tests/docs)
packages/definition/src/index.test.ts, packages/definition/README.md
Tests/imports updated (exports CommandRunner); examples/docs reference defineWithTypes/lazyWithTypes alongside define/lazy; minor test simplifications.
Bone Package Test
packages/bone/src/index.test.ts
Replaces define usage with defineWithTypes and wraps extension mappings in { extensions: ... } shape.
Docs — Guides & Config
packages/docs/src/guide/advanced/type-system.md, packages/docs/src/guide/essentials/lazy-async.md, packages/docs/src/guide/essentials/composable.md, packages/docs/src/guide/essentials/plugin-system.md, packages/docs/src/.vitepress/config.ts
Extensive doc updates adding examples for new typed helpers, revising wording and examples (lazy/command loader), minor link and alias bump (debug@4.4.1debug@4.4.3).
Plugin Example
packages/plugin-completion/examples/i18n.node.ts
Removed a // @ts-expect-error before an await cli(...) call (exposes prior suppressed type-check).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev
  participant DefAPI as defineWithTypes / define
  participant Lazy as lazyWithTypes / lazy
  participant CLI as CLI
  participant Runner as CommandRunner

  Dev->>DefAPI: Provide typed command definition
  DefAPI-->>CLI: Registered command metadata (typed args/extensions)
  Dev->>Lazy: Provide typed lazy loader
  Lazy-->>CLI: Supplies typed CommandRunner
  CLI->>Runner: run(ctx) with ctx.values + ctx.extensions
  Runner-->>CLI: result/output
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

In burrows of code where the type carrots shine,
I nibble generics, aligning each line.
defineWithTypes hops, lazyWithTypes too—
Extensions and args now clearer in view.
Tests snug and docs brushed, I thump once for cheer—🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title Check ❓ Inconclusive The title “fix: improve type-safe” is vague and does not clearly describe the primary change; while it hints at type-safety improvements, it fails to specify what was changed or which APIs were affected. Please revise the title to be more specific and clear, for example: “feat: add defineWithTypes and lazyWithTypes for improved type-safe command definitions” or similar wording that highlights the main change.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 320c868 and 8290f78.

📒 Files selected for processing (1)
  • packages/gunshi/src/cli/builtin.ts (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kazupon kazupon added improvement Includes backwards-compatible fixes ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf labels Sep 30, 2025
@pkg-pr-new

pkg-pr-new Bot commented Sep 30, 2025

Copy link
Copy Markdown

Open in StackBlitz

@gunshi/bone

npm i https://pkg.pr.new/@gunshi/bone@330

@gunshi/definition

npm i https://pkg.pr.new/@gunshi/definition@330

gunshi

npm i https://pkg.pr.new/gunshi@330

@gunshi/plugin

npm i https://pkg.pr.new/@gunshi/plugin@330

@gunshi/plugin-completion

npm i https://pkg.pr.new/@gunshi/plugin-completion@330

@gunshi/plugin-dryrun

npm i https://pkg.pr.new/@gunshi/plugin-dryrun@330

@gunshi/plugin-global

npm i https://pkg.pr.new/@gunshi/plugin-global@330

@gunshi/plugin-i18n

npm i https://pkg.pr.new/@gunshi/plugin-i18n@330

@gunshi/plugin-renderer

npm i https://pkg.pr.new/@gunshi/plugin-renderer@330

@gunshi/resources

npm i https://pkg.pr.new/@gunshi/resources@330

@gunshi/shared

npm i https://pkg.pr.new/@gunshi/shared@330

commit: 8290f78

@kazupon
kazupon marked this pull request as ready for review October 4, 2025 13:17
@kazupon
kazupon merged commit 01ec218 into main Oct 4, 2025
8 checks passed
@kazupon
kazupon deleted the fix/type-safe-experience branch October 4, 2025 13:25

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/docs/src/guide/essentials/lazy-async.md (1)

310-335: Add the missing name property to define.

In this sample, define({ ... }) omits the required name field, so the snippet won’t type-check or run as-is. Please include name in the definition (even if you override it in the lazy meta) to keep the example functional.

Apply:

-  return define({
-    description: `Config command (debug: ${isDebug})`,
+  return define({
+    name: 'config',
+    description: `Config command (debug: ${isDebug})`,
🧹 Nitpick comments (3)
packages/docs/src/.vitepress/config.ts (1)

143-143: debug@4.4.3 bump is verified safe

  • 4.4.3 exists on npm, patches the malicious 4.4.2 release; no known vulnerabilities.
    Optionally, avoid hard-coded pnpm alias paths by using dynamic package resolution.
packages/definition/src/index.test.ts (1)

3-3: Type assertion suggests incomplete type inference.

CommandRunner is imported solely for the type assertion at line 61. This suggests the loader's return type may not be sufficiently narrow, requiring manual type narrowing.

Consider improving the type inference in the lazy() function or its loader signature to eliminate the need for this type assertion, which could hide type-safety issues if the loader returns something other than a CommandRunner.

Also applies to: 61-61

packages/gunshi/src/index.ts (1)

5-8: Document the newly exported helpers.

Now that defineWithTypes/lazyWithTypes are part of the public export set, the API list in the header comment should mention them to stay in sync.

Also applies to: 29-29

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 26d9f89 and 320c868.

📒 Files selected for processing (16)
  • packages/bone/src/index.test.ts (1 hunks)
  • packages/definition/README.md (1 hunks)
  • packages/definition/src/index.test.ts (3 hunks)
  • packages/docs/src/.vitepress/config.ts (1 hunks)
  • packages/docs/src/guide/advanced/type-system.md (4 hunks)
  • packages/docs/src/guide/essentials/composable.md (1 hunks)
  • packages/docs/src/guide/essentials/lazy-async.md (7 hunks)
  • packages/docs/src/guide/essentials/plugin-system.md (1 hunks)
  • packages/gunshi/src/context.ts (1 hunks)
  • packages/gunshi/src/definition.test.ts (5 hunks)
  • packages/gunshi/src/definition.ts (5 hunks)
  • packages/gunshi/src/index.ts (1 hunks)
  • packages/gunshi/src/plugin/core.test.ts (5 hunks)
  • packages/gunshi/src/types.ts (6 hunks)
  • packages/gunshi/test/utils.ts (1 hunks)
  • packages/plugin-completion/examples/i18n.node.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • packages/plugin-completion/examples/i18n.node.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.ts: Use ES modules throughout the codebase
Follow existing code style (enforced by ESLint and Prettier)

Files:

  • packages/gunshi/test/utils.ts
  • packages/definition/src/index.test.ts
  • packages/bone/src/index.test.ts
  • packages/gunshi/src/context.ts
  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/types.ts
  • packages/gunshi/src/definition.test.ts
  • packages/gunshi/src/index.ts
  • packages/gunshi/src/definition.ts
packages/gunshi/src/**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

packages/gunshi/src/**/*.ts: All source code is in TypeScript with strict mode enabled
Type safety is a core feature - maintain strict TypeScript types throughout

Files:

  • packages/gunshi/src/context.ts
  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/types.ts
  • packages/gunshi/src/definition.test.ts
  • packages/gunshi/src/index.ts
  • packages/gunshi/src/definition.ts
🧠 Learnings (3)
📚 Learning: 2025-07-21T07:12:47.997Z
Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/test/**/*.test.ts : Use 'describe' and 'it' blocks for test organization

Applied to files:

  • packages/definition/src/index.test.ts
  • packages/gunshi/src/definition.test.ts
📚 Learning: 2025-07-21T07:12:47.997Z
Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/test/**/*.test.ts : Add tests for new features in the corresponding test file

Applied to files:

  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/definition.test.ts
📚 Learning: 2025-07-21T07:12:47.997Z
Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to **/*.ts : Use ES modules throughout the codebase

Applied to files:

  • packages/docs/src/guide/essentials/lazy-async.md
🧬 Code graph analysis (6)
packages/definition/src/index.test.ts (1)
packages/gunshi/src/definition.ts (1)
  • lazy (271-295)
packages/bone/src/index.test.ts (2)
packages/gunshi/src/definition.ts (1)
  • defineWithTypes (181-198)
packages/plugin-global/src/extension.ts (1)
  • GlobalExtension (12-41)
packages/gunshi/src/context.ts (1)
packages/gunshi/src/types.ts (3)
  • GunshiParams (40-57)
  • Args (9-9)
  • ExtendContext (28-28)
packages/gunshi/src/plugin/core.test.ts (2)
packages/gunshi/src/definition.ts (2)
  • defineWithTypes (181-198)
  • ExtendContext (60-60)
packages/gunshi/src/types.ts (1)
  • ExtendContext (28-28)
packages/gunshi/src/definition.test.ts (3)
packages/gunshi/test/utils.ts (1)
  • DeepWriteable (17-17)
packages/gunshi/src/definition.ts (8)
  • CommandRunner (58-58)
  • Command (56-56)
  • Args (53-53)
  • GunshiParams (61-61)
  • define (131-136)
  • defineWithTypes (181-198)
  • lazy (271-295)
  • lazyWithTypes (364-377)
packages/gunshi/src/types.ts (4)
  • CommandRunner (606-608)
  • Command (507-558)
  • Args (9-9)
  • GunshiParams (40-57)
packages/gunshi/src/definition.ts (1)
packages/gunshi/src/types.ts (11)
  • GunshiParamsConstraint (74-82)
  • DefaultGunshiParams (65-65)
  • Command (507-558)
  • Prettify (21-21)
  • Args (9-9)
  • ExtractArgs (91-99)
  • ExtractExtensions (115-117)
  • ExtendContext (28-28)
  • CommandLoader (617-619)
  • LazyCommand (567-581)
  • GunshiParams (40-57)
🪛 markdownlint-cli2 (0.18.1)
packages/docs/src/guide/advanced/type-system.md

192-192: Blank line inside blockquote

(MD028, no-blanks-blockquote)

🔇 Additional comments (2)
packages/definition/src/index.test.ts (2)

44-44: LGTM! Improved type-safety.

The removal of type-casting in the command.run(ctx) call demonstrates better type inference in the updated API.


49-49: Verify the functional change in the lazy factory.

The lazy factory now wraps the return value with String(ctx.values.foo) instead of returning ctx.values.foo directly. While the test still passes, this changes the semantics of the test case.

Ensure this reflects the intended usage pattern for lazy commands or consider reverting to the simpler pattern if the String coercion is not necessary.

Comment thread packages/definition/src/index.test.ts
Comment thread packages/gunshi/src/definition.ts
Comment thread packages/gunshi/test/utils.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Includes backwards-compatible fixes ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant