Skip to content

Conversation

@zhangxuhe1
Copy link
Contributor

What & Why

What: Comprehensive refactoring to eliminate explicit any types across Dify services and improve type safety
Why: Resolves linting errors and enhances code maintainability by replacing unsafe type usage with proper TypeScript types

Fixes #193

Pre-PR Checklist

Run these:

  • pnpm type-check
  • pnpm format:check
  • pnpm lint (Dify services now clean, other pre-existing issues remain)
  • pnpm build
  • pnpm i18n:check (not applicable)

Type

  • 🐛 Bug fix
  • ✨ Feature
  • 💥 Breaking change
  • 📚 Docs
  • ♻️ Refactor
  • ⚡ Performance

Key Changes

Type Safety Improvements

  • lib/services/dify/types.ts: Replaced Record<string, any> with Record<string, unknown> for better type safety
  • Array Types: Converted any[] to properly typed arrays (DifyRetrieverResource[], DifyMessageFile[])
  • Error Handling: Standardized error types to use Error instead of any where possible
  • Data Extraction: Added type-safe data extraction with runtime validation
  • Interface Simplification: Used type aliases to simplify interface inheritance

Service Layer Improvements

  • Chat Service: Enhanced streaming with proper event type handling
  • Completion Service: Improved error handling with type safety
  • Hooks: Updated file handling with proper ChatUploadFile type assertions
  • Import Optimization: Converted require() imports to ES6 imports for ESLint compliance

Maintained Compatibility

  • DifyApiError: Kept necessary any type for API compatibility with external Dify services
  • Type Assertions: Added proper type assertions where runtime validation is needed
  • Backward Compatibility: All changes maintain existing API surface

Technical Details

  • All TypeScript compilation passes without errors
  • Build process completes successfully
  • Linting errors in Dify services eliminated (364 → 0 for target files)
  • No breaking changes to public APIs
  • Performance impact: minimal, type checking occurs at build time

This refactoring significantly improves code maintainability and type safety while preserving all existing functionality. The focused approach on Dify services creates a foundation for similar improvements across the codebase.

- Replace Record<string, any> with Record<string, unknown> for better type safety
- Replace any[] with proper typed arrays (DifyRetrieverResource[], DifyMessageFile[])
- Standardize error types to use Error instead of any
- Add type-safe data extraction with runtime validation
- Simplify interface inheritance using type aliases
- Fix ESLint require() imports by converting to ES6 imports
- Remove unused imports and variables to satisfy linting rules
- Maintain backward compatibility through proper type assertions
- Keep necessary any type in DifyApiError for API compatibility

Affected files:
- lib/services/dify/types.ts: Core type definitions with Record<string, unknown>
- lib/services/dify/chat-service.ts: Chat streaming with proper event types
- lib/services/dify/completion-service.ts: Completion service error handling
- lib/hooks/use-chat-interface.ts: File handling with ChatUploadFile type assertions
- lib/hooks/use-create-conversation.ts: Store imports optimization

All TypeScript validation and linting passes, improving code maintainability.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. type:refactor Internal code improvements without behavior change. labels Aug 11, 2025
@zhangxuhe1 zhangxuhe1 requested a review from lyzno1 August 11, 2025 06:53
@lyzno1 lyzno1 requested a review from Copilot August 11, 2025 06:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR eliminates explicit any types across Dify services to improve type safety and resolve linting errors. The refactoring replaces unsafe type usage with proper TypeScript types while maintaining full API compatibility.

  • Replaced Record<string, any> with Record<string, unknown> for better type safety
  • Converted any[] to properly typed arrays (DifyRetrieverResource[], DifyMessageFile[])
  • Enhanced error handling with proper Error types instead of any

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lib/services/dify/types.ts Core type definitions updated to use Record<string, unknown> and proper array types
lib/services/dify/completion-service.ts Enhanced error handling with proper Error type conversion
lib/services/dify/chat-service.ts Improved streaming with type-safe event handling and data extraction
lib/hooks/use-create-conversation.ts Added proper type imports and removed dynamic require() calls
lib/hooks/use-chat-interface.ts Type-safe file handling with proper ChatUploadFile assertions

zhangxuhe1 and others added 3 commits August 11, 2025 15:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@lyzno1 lyzno1 merged commit 8c773a6 into main Aug 11, 2025
14 checks passed
@dosubot dosubot bot added the lgtm Looks good to me; approved by a reviewer. label Aug 11, 2025
@zhangxuhe1 zhangxuhe1 deleted the refactor/eliminate-any-types branch August 11, 2025 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm Looks good to me; approved by a reviewer. size:L This PR changes 100-499 lines, ignoring generated files. type:refactor Internal code improvements without behavior change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

resolve types any lint error

3 participants