Skip to content

fix: improvement plugin type inference - #262

Merged
kazupon merged 3 commits into
mainfrom
fix/plugin-typing
Jul 28, 2025
Merged

kazupon merged 3 commits into
mainfrom
fix/plugin-typing

Conversation

@kazupon

@kazupon kazupon commented Jul 28, 2025

Copy link
Copy Markdown
Owner

Description

Linked Issues

Additional context

Summary by CodeRabbit

  • New Features

    • Improved type safety and inference for plugins with dependencies and extensions, enabling more precise typing of plugin contexts and extensions.
    • Plugins now explicitly declare dependencies as constants, enhancing maintainability and clarity.
  • Refactor

    • Streamlined and simplified plugin type declarations and extension functions for better developer experience.
    • Removed unnecessary type assertions and unused imports across multiple plugins.
    • Updated package description for the plugin development kit.
  • Tests

    • Added comprehensive type-level tests to ensure correctness of plugin type utilities and options.

@coderabbitai

coderabbitai Bot commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This update introduces advanced TypeScript type utilities and generics to the core plugin system, refactors plugin declarations in several packages to leverage enhanced type inference, and adds comprehensive type-level tests. The changes focus on improving static type safety for plugin dependencies and extensions, with no alterations to runtime logic.

Changes

Cohort / File(s) Change Summary
Core Plugin Type Utilities and Typings
packages/gunshi/src/plugin/core.ts
Introduces advanced type utilities for plugin dependencies and extensions, refactors PluginOptions and plugin function with generics for improved type inference and merging of extensions.
Type-level Plugin Tests
packages/gunshi/src/plugin/core.test-d.ts
Expands from a single test to a comprehensive suite of type-level tests covering new and existing type utilities, dependency extraction, option inference, and extension merging.
Plugin Function Type Assertion Tests
packages/gunshi/src/plugin/core.test.ts
Removes usage of an imported type in type assertions, replacing with inline object types for clarity and directness in test assertions.
Plugin Completion Typings Refactor
packages/plugin-completion/src/index.ts
Centralizes dependency declaration, adds explicit generic parameters to the plugin call, and refines typings for plugin context and dependencies.
Plugin I18n Typings Refactor
packages/plugin-i18n/src/index.ts
Casts dependencies array as an immutable tuple (as const), removes explicit type assertions for extension return value, relying on inference.
Plugin Renderer Typings Refactor
packages/plugin-renderer/src/index.ts
Removes unused imports and internal types, centralizes and types dependencies, updates plugin and extension typings, and simplifies extension and setup method signatures.
Plugin Package Metadata
packages/plugin/package.json
Updates the package description to "plugin development kit for gunshi"; no functional or dependency changes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PluginFactory as plugin()
    participant PluginOptions
    participant DependencyTypeUtils

    User->>PluginFactory: Call plugin<TDeps, TId, TExt, ...>(options)
    PluginFactory->>DependencyTypeUtils: Infer dependency extensions/types
    DependencyTypeUtils-->>PluginFactory: Return merged extension types
    PluginFactory->>PluginOptions: Construct plugin with inferred types
    PluginOptions-->>PluginFactory: Return strongly-typed plugin
    PluginFactory-->>User: Return plugin with merged extension typings
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

In the warren of types, we hop and we bound,
With generics and tests, new safety is found.
Extensions now merge, dependencies clear,
Type rabbits rejoice—no more type errors here!
🐇✨ Type-safe fields, our code’s delight,
We nibble on carrots and get typings right!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/plugin-typing

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@kazupon kazupon added the improvement Includes backwards-compatible fixes label Jul 28, 2025
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 28, 2025

Copy link
Copy Markdown

Deploying gunshi with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9bfc606
Status: ✅  Deploy successful!
Preview URL: https://77524828.gunshi.pages.dev
Branch Preview URL: https://fix-plugin-typing.gunshi.pages.dev

View logs

@pkg-pr-new

pkg-pr-new Bot commented Jul 28, 2025

Copy link
Copy Markdown

Open in StackBlitz

@gunshi/bone

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

@gunshi/definition

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

gunshi

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

@gunshi/plugin

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

@gunshi/plugin-completion

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

@gunshi/plugin-dryrun

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

@gunshi/plugin-global

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

@gunshi/plugin-i18n

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

@gunshi/plugin-renderer

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

@gunshi/resources

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

@gunshi/shared

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

commit: 9bfc606

@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: 1

🧹 Nitpick comments (2)
packages/gunshi/src/plugin/core.ts (2)

115-176: Sophisticated type system for plugin extensions

The enhanced PluginOptions interface provides excellent type safety by computing and flowing extension types through all plugin callbacks. While the generic parameters add complexity, they ensure compile-time verification of plugin compatibility.

Consider documenting these complex type parameters with examples to help plugin developers understand the type flow.


320-321: Consider adding a comment explaining the any type usage

While the any type is necessary here to handle all overloads, consider adding a comment explaining why it's safe in this context to help future maintainers.

 // eslint-disable-next-line @typescript-eslint/no-explicit-any
+// Implementation uses 'any' to handle all overload signatures - type safety is enforced at the API level
 export function plugin(options: any = {}): any {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b144c79 and 9bfc606.

📒 Files selected for processing (7)
  • packages/gunshi/src/plugin/core.test-d.ts (1 hunks)
  • packages/gunshi/src/plugin/core.test.ts (3 hunks)
  • packages/gunshi/src/plugin/core.ts (5 hunks)
  • packages/plugin-completion/src/index.ts (1 hunks)
  • packages/plugin-i18n/src/index.ts (2 hunks)
  • packages/plugin-renderer/src/index.ts (2 hunks)
  • packages/plugin/package.json (1 hunks)
🧰 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/plugin-completion/src/index.ts
  • packages/plugin-i18n/src/index.ts
  • packages/gunshi/src/plugin/core.test-d.ts
  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/plugin/core.ts
  • packages/plugin-renderer/src/index.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/plugin/core.test-d.ts
  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/plugin/core.ts
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : Type safety is a core feature - maintain strict TypeScript types throughout
Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled
packages/plugin/package.json (2)

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled

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

packages/plugin-i18n/src/index.ts (3)

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : Type safety is a core feature - maintain strict TypeScript types throughout

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled

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

packages/gunshi/src/plugin/core.test-d.ts (8)

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

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 : Test files are in 'packages/gunshi/test/' with '.test.ts' extension

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 : Mock external dependencies when needed in tests

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

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : Type safety is a core feature - maintain strict TypeScript types throughout

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 : Snapshot tests are used for renderer output validation

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled

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

packages/gunshi/src/plugin/core.test.ts (8)

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 : Test files are in 'packages/gunshi/test/' with '.test.ts' extension

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

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : Type safety is a core feature - maintain strict TypeScript types throughout

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 : Mock external dependencies when needed in tests

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled

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

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 : Snapshot tests are used for renderer output validation

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

packages/gunshi/src/plugin/core.ts (5)

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : Type safety is a core feature - maintain strict TypeScript types throughout

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled

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

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 : Test files are in 'packages/gunshi/test/' with '.test.ts' extension

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 : Mock external dependencies when needed in tests

packages/plugin-renderer/src/index.ts (4)

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : All source code is in TypeScript with strict mode enabled

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 : Snapshot tests are used for renderer output validation

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 : Test files are in 'packages/gunshi/test/' with '.test.ts' extension

Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/src/**/*.ts : Type safety is a core feature - maintain strict TypeScript types throughout

🧬 Code Graph Analysis (2)
packages/plugin-i18n/src/index.ts (2)
packages/shared/src/utils.ts (1)
  • namespacedId (77-81)
packages/plugin-i18n/src/types.ts (2)
  • I18nCommandContext (41-74)
  • I18nPluginOptions (79-92)
packages/gunshi/src/plugin/core.test-d.ts (1)
packages/gunshi/src/plugin/core.ts (8)
  • ExtractDependencyId (27-31)
  • PluginDependency (77-87)
  • IsOptionalDependency (37-41)
  • InferDependencyExtensions (61-71)
  • PluginOptions (133-176)
  • PluginFunction (93-95)
  • PluginExtension (101-104)
  • OnPluginExtension (110-113)
🔇 Additional comments (17)
packages/plugin/package.json (1)

3-3: LGTM! Enhanced description reflects improved plugin capabilities.

The updated description "plugin development kit for gunshi" better represents the package's purpose than the previous "utilities for gunshi plugin", especially given the type inference improvements introduced in this PR.

packages/plugin-i18n/src/index.ts (2)

108-108: Excellent type safety improvement with as const.

Using as const ensures the dependencies array is treated as an immutable tuple with literal types, enabling precise type inference in the plugin system. This aligns with the enhanced plugin typing introduced in the core.


187-187: Good removal of explicit type assertion.

Allowing TypeScript to infer the return type naturally is more robust than explicit type assertions and provides better type safety. The compiler can now verify the return type matches the expected interface.

packages/plugin-completion/src/index.ts (2)

39-40: Consistent dependency declaration pattern.

The as const assertion on the dependencies array follows the same pattern established in other plugins, ensuring proper type inference and immutability. Good practice to centralize the dependency declaration.


48-56: Excellent explicit generic typing.

The explicit generic parameters enhance type safety by specifying:

  • Command context mapping for extensions
  • Plugin ID for proper identification
  • Dependencies for type inference

This leverages the improved plugin typing system introduced in the core.

packages/gunshi/src/plugin/core.test.ts (3)

10-10: Good cleanup of unused import.

Removing the ExtendContext import after replacing it with inline types in the test assertions is proper housekeeping.


204-206: Appropriate inline type assertions.

Replacing ExtendContext with explicit inline object types makes the test assertions more explicit and focused on the specific extension properties being tested. This maintains type safety while improving readability.

Also applies to: 212-214


239-239: Consistent type assertion pattern.

The inline { auth: {} } type assertion follows the same pattern as the earlier changes, maintaining consistency throughout the test file.

Also applies to: 244-244

packages/gunshi/src/plugin/core.test-d.ts (5)

1-13: Excellent comprehensive type testing setup.

The imports are well-organized and cover all the necessary type utilities from core.ts. The switch to Vitest's expectTypeOf for type-level testing is appropriate for validating the enhanced plugin type system.


15-30: Thorough testing of ExtractDependencyId utility.

The tests cover all the key scenarios:

  • Generic PluginDependency interface
  • Specific object with id
  • Object with id and optional flag

This validates that the type utility correctly extracts dependency IDs from various input forms.


32-49: Comprehensive IsOptionalDependency testing.

The test cases properly validate the optional dependency detection logic, covering both explicit true/false cases and the default behavior. This ensures the type system correctly identifies optional dependencies.


56-115: Excellent coverage of InferDependencyExtensions complexity.

The test suite covers critical scenarios:

  • Required and optional dependencies mixing
  • Unavailable dependencies handling
  • Empty dependencies edge case
  • Mixed string and object dependency forms
  • Long dependency chains

This thoroughly validates the recursive type inference logic that's central to the plugin system.


117-245: Comprehensive PluginOptions generic validation.

The tests validate all aspects of the complex generic PluginOptions interface:

  • Default type parameters
  • Fully specified generics
  • Required and optional dependency mixing
  • Unmatched dependencies handling
  • No dependencies edge case

This ensures the plugin options typing works correctly across all usage patterns. The commented note about toEqualTypeOf limitations shows good awareness of testing constraints.

packages/plugin-renderer/src/index.ts (2)

55-56: Correct implementation of typed dependencies

The explicit declaration of dependencies as a const-asserted array enables proper type inference in the enhanced plugin system.


124-128: Clean simplification leveraging improved type inference

The removal of explicit generic type annotations demonstrates that the enhanced type system now properly infers types throughout the decorator chain.

packages/gunshi/src/plugin/core.ts (2)

23-72: Well-crafted type utilities for dependency inference

The type utilities correctly implement recursive type inference for plugin dependencies with proper handling of optional dependencies. The use of tail recursion pattern ensures TypeScript can optimize the type computation.


217-312: Type-safe plugin factory overloads

The overloaded signatures correctly distinguish between plugins with and without extensions, ensuring proper typing based on the presence of the extension property.


return {
text: localizable(ctx, cmd, i18n?.translate),
text: localizable(ctx as unknown as CommandContext, cmd, i18n?.translate),

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.

💡 Verification agent

🧩 Analysis chain

Verify the necessity of type casting to CommandContext

The cast ctx as unknown as CommandContext suggests a type mismatch. Consider if the localizable function's type signature could be adjusted to accept the actual context type instead.


🏁 Script executed:

#!/bin/bash
# Search for the localizable function definition to understand its type requirements
ast-grep --pattern 'export $_ function localizable($_) {
  $$$
}'

# Also check for any type definitions
rg -A 5 "localizable.*CommandContext"

Length of output: 475


🏁 Script executed:

#!/bin/bash
# Show import statements in plugin-renderer/src/index.ts
echo "Import statements in plugin-renderer/src/index.ts:"
sed -n '1,50p' packages/plugin-renderer/src/index.ts | rg "import"

# Search for export of localizable function across repo
echo "Searching for export of localizable function:"
rg -n "export .*localizable" -n .

# Search for any standalone definition of localizable
echo "Searching for any function localizable definition:"
rg -n "function localizable" -n .

Length of output: 1123


🏁 Script executed:

#!/bin/bash
# Display lines around the localizable function definition to inspect its signature
echo "Context around localizable definition in packages/shared/src/localization.ts:"
sed -n '1,200p' packages/shared/src/localization.ts

Length of output: 3015


Update localizable signature to support extended contexts

The cast ctx as unknown as CommandContext indicates that localizable only accepts the base CommandContext, despite having a generic C for extended contexts. We should change its signature so callers can pass in any context that extends CommandContext without casting.

Suggested diff in packages/shared/src/localization.ts:

-export function localizable<
-  A extends Args,
-  C = {}, // for CommandContext
-  E extends Record<string, string> = {}, // for extended resources
-  K = ResolveTranslationKeys<A, C, E>
->(
-  ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K>
+export function localizable<
+  A extends Args,
+  C extends CommandContext = CommandContext, // allow extended contexts
+  E extends Record<string, string> = {},    // for extended resources
+  K = ResolveTranslationKeys<A, C, E>
+>(
+  ctx: C,
+  cmd: Command,
+  translate?: Translation<A, C, E, K>
 ): Localization<A, C, E> {
   …

And in packages/plugin-renderer/src/index.ts, you can then drop the cast:

- text: localizable(ctx as unknown as CommandContext, cmd, i18n?.translate),
+ text: localizable(ctx, cmd, i18n?.translate),

Files to update:

  • packages/shared/src/localization.ts
  • packages/plugin-renderer/src/index.ts
🤖 Prompt for AI Agents
In packages/shared/src/localization.ts, update the localizable function
signature to accept a generic context type C that extends CommandContext,
allowing it to handle extended contexts without casting. Then, in
packages/plugin-renderer/src/index.ts at line 118, remove the cast from ctx when
calling localizable, passing ctx directly. This change ensures type safety and
cleaner code by leveraging generics properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Includes backwards-compatible fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant