angular-realworld-example-app icon indicating copy to clipboard operation
angular-realworld-example-app copied to clipboard

fix: Improve type safety and API consistency in ArticlesService

Open xingarr opened this issue 3 months ago • 2 comments

Description

This PR improves code quality by removing TypeScript suppressions, fixing type inconsistencies, and enhancing test quality across the codebase.

Changes Made

1. Type Safety Improvements

  • Removed @ts-ignore suppression in ArticlesService.query() method
  • Added proper type casting using keyof typeof for dynamic filter property access
  • Added undefined value checking to prevent setting invalid HTTP parameters
  • Explicit type conversion for all parameter values to strings

2. API Consistency Fix

  • Fixed unfavorite() return type from Observable<void> to Observable<Article>
  • Added response unwrapping to match RealWorld API specification
  • Improved consistency with favorite() method behavior

3. Test Quality Enhancements

  • Removed as any type assertions across all test files
  • Replaced with proper type checking using in operator
  • Added test coverage for undefined filter value handling
  • Updated unfavorite test to verify proper Article response

Files Modified

  • src/app/features/article/services/articles.service.ts
  • src/app/features/article/services/articles.service.spec.ts
  • src/app/features/profile/services/profile.service.spec.ts
  • src/app/features/article/services/comments.service.spec.ts
  • src/app/features/article/services/tags.service.spec.ts
  • src/app/core/auth/services/jwt.service.spec.ts

Why This Matters

  • Better type safety - Catches potential bugs at compile time
  • Improved maintainability - Code is easier to understand and refactor
  • API consistency - Methods behave predictably and match API spec
  • Higher code quality - Follows TypeScript and Angular best practices
  • Better test coverage - More robust and type-safe tests

Testing

  • ✅ Build passes successfully (npm run build)
  • ✅ No TypeScript compilation errors
  • ✅ All changes maintain backward compatibility
  • ✅ Enhanced test coverage for edge cases

Breaking Changes

None - all changes are backward compatible.

Related Issues

Addresses code quality improvements by:

  • Removing TypeScript type suppressions
  • Fixing API response type inconsistencies
  • Improving test quality and type safety

xingarr avatar Nov 05 '25 01:11 xingarr

Sorry, can you resolve conflicts?

c4ffein avatar Nov 05 '25 16:11 c4ffein

Thanks for your review, I have just fixed coflicts, could you please check it again and merge it? Thanks and regards

xingarr avatar Nov 05 '25 17:11 xingarr