-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: 为useVbenVxeGrid添加泛型声明,使grid实例上能正确获取到行数据的类型 #5653
base: main
Are you sure you want to change the base?
Conversation
|
WalkthroughThe changes introduce generic type parameters to enhance type safety and flexibility in the grid API, interfaces, and hook functions. Updates include modifying class signatures, property types, method signatures, and interface definitions to accept customizable types. Additionally, a new adapter wrapper is provided to re-export the grid hook with improved type inference, and playground examples are updated to explicitly specify type parameters. Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
playground/src/views/examples/vxe-table/basic.vue (1)
47-55
: Helpful commented code example for form schemaThe commented form configuration serves as a useful reference for developers who might need to implement form functionality in the future. Consider adding a brief documentation comment explaining when this form configuration would be useful.
- // 放开注释查看表单组件的类型 + // 放开注释启用表单组件并查看其类型。适用于需要在表格上方添加搜索或筛选表单的场景。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
packages/effects/plugins/src/vxe-table/api.ts
(2 hunks)packages/effects/plugins/src/vxe-table/types.ts
(3 hunks)packages/effects/plugins/src/vxe-table/use-vxe-grid.ts
(2 hunks)playground/src/adapter/vxe-table.ts
(2 hunks)playground/src/views/examples/vxe-table/basic.vue
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: post-update (windows-latest)
- GitHub Check: post-update (ubuntu-latest)
🔇 Additional comments (19)
playground/src/views/examples/vxe-table/basic.vue (2)
46-58
: Great addition of generic type parameter for improved type safety!The use of the generic type parameter
<RowType>
withuseVbenVxeGrid
now provides proper type inference for grid row data. This enhancement ensures that operations on grid rows will have proper type checking, making the code safer and enabling better IDE auto-completion.
60-61
: Useful debugging comment for type inspectionThis comment provides valuable guidance for developers who need to inspect the table instance's type structure. This is particularly helpful for understanding the available properties and methods on the grid instance when working with TypeScript.
playground/src/adapter/vxe-table.ts (3)
3-3
: Good addition of ComponentType importAdding the
ComponentType
import is necessary for the generic type implementation that follows. This creates a clean separation between component types and data types in the grid implementation.
9-12
: Clean import renaming for implementation clarityRenaming the imported function to
useGrid
while exporting a wrapper asuseVbenVxeGrid
creates a clear distinction between the core implementation and the adapter. This is a good practice for maintaining a clean API surface.
237-239
: Well-implemented generic wrapper functionThis type-safe wrapper implementation is excellent:
- It correctly constrains
T
toRecord<string, any>
which is appropriate for row data- It uses
Parameters<typeof useGrid<T, ComponentType>>
to ensure parameter compatibility- It correctly forwards all parameters to the underlying function
This pattern allows consumers to specify row types while maintaining a consistent form component type.
packages/effects/plugins/src/vxe-table/use-vxe-grid.ts (4)
1-2
: Good import of BaseFormComponentTypeAdding the import for
BaseFormComponentType
is necessary for the generic implementation. This type will be used as a constraint and default for the form component type parameter.
12-15
: Well-structured generic type parametersThe implementation of generic type parameters is well done:
T extends Record<string, any> = any
provides proper constraint with backward compatibilityD extends BaseFormComponentType = BaseFormComponentType
offers flexibility for form component types- The parameter
options: VxeGridProps<T, D>
correctly uses both type parametersThis structure allows for flexible typing while maintaining backward compatibility.
18-18
: Properly typed extendedApiThe
extendedApi
is correctly typed asExtendedVxeGridApi<T, D>
, ensuring that the generic parameters are properly propagated through the API chain. This maintains type safety throughout the codebase.
24-24
: Correct typing for props parameterThe
props
parameter in thedefineComponent
function is properly typed asVxeGridProps<T>
, ensuring that the component receives correctly typed props. This completes the type safety chain from function call to component rendering.packages/effects/plugins/src/vxe-table/api.ts (3)
29-29
: Good implementation of generic type parameter for VxeGridApiAdding the generic type parameter
<T extends Record<string, any> = any>
to theVxeGridApi
class is a key enhancement that enables proper type inference throughout the grid API. The constraint and default value ensure backward compatibility.
33-36
: Proper typing of class propertiesThe updates to class property types are correct:
grid
asVxeGridInstance<T>
state
asnull | VxeGridProps<T>
store
asStore<VxeGridProps<T>>
These changes ensure that the row data type information flows through all aspects of the grid instance.
101-104
: Correctly updated setState method parameter typeThe
setState
method's parameter type has been properly updated to use the generic type parameter:((prev: VxeGridProps<T>) => Partial<VxeGridProps<T>>) | Partial<VxeGridProps<T>>This ensures type consistency when updating grid state, preventing potential type errors.
packages/effects/plugins/src/vxe-table/types.ts (7)
12-12
: Updated import to include BaseFormComponentType.The import now correctly includes
BaseFormComponentType
, which is necessary for the generic type constraints being added to the interfaces.
34-37
: Good enhancement - generic type parameters improve type safety.Adding generic parameters to
VxeGridProps
is a good improvement that allows consumers to specify the row data type (T
) and form component type (D
). The constraints are appropriate - row data extendsRecord<string, any>
and form components extendBaseFormComponentType
.
57-57
: Correctly updated gridOptions to use generic type.The
gridOptions
property now correctly uses the generic type parameterT
, which will allow for proper type inference when accessing grid data.
61-61
: Correctly updated gridEvents to use generic type.The
gridEvents
property now correctly uses the generic type parameterT
, ensuring event handlers receive properly typed data.
65-65
: Correctly updated formOptions to use generic type.The
formOptions
property now properly uses the generic type parameterD
, which improves type safety for form components.
29-32
:✅ Verification successful
Verify all uses of VxeTableGridOptions are updated consistently.
The
VxeTableGridOptions
interface is now using a generic parameter<T = any>
. Since you're updating theVxeGridProps
interface to use this generic, ensure that all other places usingVxeTableGridOptions
are consistently updated throughout the codebase.
🏁 Script executed:
#!/bin/bash # Search for all uses of VxeTableGridOptions to ensure consistent generic usage echo "Searching for VxeTableGridOptions usage..." rg -t ts "VxeTableGridOptions" -A 1 -B 1Length of output: 1754
Generic Parameter Usage Verified
After verifying the codebase, all references to
VxeTableGridOptions
now consistently use the generic parameter as defined inpackages/effects/plugins/src/vxe-table/types.ts
. In particular:
- The interface in
packages/effects/plugins/src/vxe-table/types.ts
is updated with<T = any>
.- The re-export in
packages/effects/plugins/src/vxe-table/index.ts
correctly reflects this change.- Usages in
playground/src/views/system/dept/data.ts
andplayground/src/views/system/menu/data.ts
explicitly pass the expected generic, ensuring consistency.No further changes are required.
72-78
:❓ Verification inconclusive
Verify that NoInfer utility type is defined.
The
ExtendedVxeGridApi
now uses generic parameters to provide better type inference. However, the code uses aNoInfer<T>
utility type that doesn't appear to be imported in this file. This could lead to TypeScript errors if the type isn't defined elsewhere.Also, in the useStore function, you've used
VxeGridProps<any, any>
for the state parameter but more specific typesVxeGridProps<D, F>
for the return type inference. Consider using consistent typing:- useStore: <T = NoInfer<VxeGridProps<D, F>>>( - selector?: (state: NoInfer<VxeGridProps<any, any>>) => T, + useStore: <T = NoInfer<VxeGridProps<D, F>>>( + selector?: (state: NoInfer<VxeGridProps<D, F>>) => T,
🏁 Script executed:
#!/bin/bash # Search for NoInfer type definition in the codebase echo "Searching for NoInfer type definition..." rg -t ts "type\s+NoInfer" --no-filename rg -t ts "interface\s+NoInfer" --no-filenameLength of output: 179
Action Required: Verify inclusion of
NoInfer
and align typing inuseStore
- The search for the
NoInfer
utility type yielded no results. Please double-check whetherNoInfer
is defined in a global declaration file (such as in aglobal.d.ts
), imported from an external library, or if it needs to be added.- In the
useStore
function, the parameter forselector
currently usesNoInfer<VxeGridProps<any, any>>
while the default generic is based onNoInfer<VxeGridProps<D, F>>
. For consistency and better type safety, update theselector
function’s parameter type accordingly.- useStore: <T = NoInfer<VxeGridProps<D, F>>>( - selector?: (state: NoInfer<VxeGridProps<any, any>>) => T, + useStore: <T = NoInfer<VxeGridProps<D, F>>>( + selector?: (state: NoInfer<VxeGridProps<D, F>>) => T,
Description
Type of change
Please delete options that are not relevant.
pnpm-lock.yaml
unless you introduce a new test example.Checklist
pnpm run docs:dev
command.pnpm test
.feat:
,fix:
,perf:
,docs:
, orchore:
.Summary by CodeRabbit
New Features
Refactor