types: add types to useProperties, useProperty and useSaveLocal#1265
Conversation
WalkthroughThis update enhances type safety and public API clarity across several packages. Type annotations are added or refined in multiple TypeScript files, notably in composables and type definition files, ensuring stricter and more explicit type usage. Two package index files now re-export internal types, making them accessible from the package root. Additionally, optional chaining is introduced in one composable to prevent runtime errors when accessing potentially undefined properties. No business logic or control flow is changed; all modifications focus on type declarations, exports, and null-safety. Changes
Poem
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
✨ 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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/settings/props/src/composable/useProperty.ts (1)
29-29: Good use of parameter destructuring with types.Nice work using the underscore notation for the unused
defaultValueparameter. Consider adding a comment explaining why this parameter is accepted but not used.-const addPropertyLinks = ({ linked, propertyName, componentProperties, defaultValue: _ }: AddPropertyLinksOptions) => { +const addPropertyLinks = ({ linked, propertyName, componentProperties, defaultValue: _ /* Received but not used */ }: AddPropertyLinksOptions) => {packages/settings/props/src/composable/useProperties.ts (1)
157-159: Consider using optional chaining here.For consistency with the rest of the codebase, change to optional chaining for the id property access.
useCanvas().operateNode({ type: 'changeProps', - id: properties.schema?.id || '', + id: properties.schema?.id ?? '', value: { props: newProps }, option: { overwrite: true } })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
packages/canvas/DesignCanvas/src/api/types.ts(1 hunks)packages/canvas/index.ts(1 hunks)packages/plugins/materials/index.ts(1 hunks)packages/settings/props/src/composable/useProperties.ts(6 hunks)packages/settings/props/src/composable/useProperty.ts(4 hunks)packages/toolbars/generate-code/src/composable/useSaveLocal.ts(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/toolbars/generate-code/src/composable/useSaveLocal.ts (1)
packages/canvas/render/src/canvas-function/page-switcher.ts (1)
currentPage(9-13)
🪛 GitHub Actions: Push And Create PR Check
packages/plugins/materials/index.ts
[error] 1-1: Build failed: Could not resolve "./src/composable/types" from "index.ts".
🪛 Biome (1.9.4)
packages/settings/props/src/composable/useProperties.ts
[error] 159-160: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
🔇 Additional comments (25)
packages/canvas/index.ts (1)
33-33: Good addition of type exportsThis export statement makes the types defined in
./DesignCanvas/src/api/typesdirectly accessible to consumers of this package, which improves the developer experience and type-safety of the API.packages/toolbars/generate-code/src/composable/useSaveLocal.ts (2)
22-23: Good use of optional chaining for type safetyThe addition of optional chaining (
?.) when accessingpageState.currentPage.idandpageState.currentPage.namealigns with the updated type definition inPageStatewherecurrentPagecan be null. This prevents potential runtime errors.
55-56: Good use of optional chaining for null safetySimilar to the previous instance, adding optional chaining here protects against null reference errors when
currentPageis null or undefined. This is a good defensive programming practice.packages/canvas/DesignCanvas/src/api/types.ts (2)
7-7: Improved type specificity for currentSchemaChanging
currentSchemafrom a genericunknowntype to a more specific object type with a requiredidproperty improves type safety and makes the code more self-documenting.
9-11: Enhanced types with nullable currentPage and added optional fieldsThe changes to
currentPagetype and addition ofcurrentPageIdandcurrentPageNameproperties:
- Make
currentPageexplicitly nullable, which is aligned with the optional chaining used elsewhere- Add specific optional properties to access page ID and name directly
- Improve overall type safety and developer experience
This aligns well with the changes in
useSaveLocal.tswhere these properties are accessed with optional chaining.packages/settings/props/src/composable/useProperty.ts (7)
16-17: Good addition of type imports.The added imports for PageState, Property, and Linked types provide the foundation for the TypeScript enhancements in this file.
21-26: Well-defined interface for addPropertyLinks options.Good practice defining a clear interface for the options object. The
defaultValueparameter is marked as unknown, which is appropriate for a value that could be of any type.
53-57: Clear interface definition for findLinked options.The FindLinkedOptions interface clearly defines the structure for the options passed to findLinked. The
blockPropertiestype uses a complex but precise type definition withProperty['content'][number][].
60-62: Well-refactored function signature with proper typing.Good job consolidating parameters into a single options object with proper typing. This improves readability and type safety.
78-78: Added proper type annotation to resetLink function.The explicit typing of the
propertiesparameter asProperty[]enhances type safety.
90-90: Good type annotation for getProperty function.The function parameter is properly typed with destructured object pattern and PageState type.
95-95: Appropriate use of type assertions.The type assertions to
Property[]are necessary here to ensure type compatibility with the functions being called.Also applies to: 99-102
packages/settings/props/src/composable/useProperties.ts (13)
16-16: Good addition of type imports.The added imports for Property and Schema types provide the foundation for the TypeScript enhancements in this file.
21-21: Enhanced type safety for getSlotSwitch.The function parameters now have proper type annotations with appropriate defaults.
56-56: Improved type safety for mergeProps.The function parameters now have proper type annotations with appropriate defaults.
75-75: Added type annotation to translateProp.The value parameter is properly typed as an object with a type property.
88-91: Well-typed reactive state.The shallowReactive properties object now has proper type annotations with nullable Schema types, which enables better type checking throughout the codebase.
93-94: Added type safety to isPageOrBlock.The function parameter is now properly typed, and optional chaining is used for safe property access.
96-96: Enhanced type safety for getProps.Both parameters are properly typed as Schema objects.
119-119: Added type annotations to setProp.The parameters now have proper type annotations, with value as unknown to accommodate different types.
139-139: Improved null safety with fallback empty string.Good practice to provide a fallback empty string for the id property when it might be undefined.
147-148: Enhanced null safety in getProp.The function now uses optional chaining and nullish coalescing to safely access potentially undefined properties.
151-152: Added null safety to delProp.Optional chaining is used to safely access the props object.
165-167: Added type safety to setProps.The schema parameter is now properly typed as a Schema object.
169-171: Enhanced type safety for getSchema.The parent parameter is now properly typed as a boolean.
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
Refactor
Chores