Skip to content

Feature: Add Typescript#34

Open
grosuigor wants to merge 1 commit into
gridu:masterfrom
grosuigor:feature/typescript
Open

Feature: Add Typescript#34
grosuigor wants to merge 1 commit into
gridu:masterfrom
grosuigor:feature/typescript

Conversation

@grosuigor

Copy link
Copy Markdown

Add full TypeScript type coverage for Observable/Observer capstone

Summary

This PR migrates the capstone solution from untyped JavaScript to fully typed TypeScript, satisfying the readme acceptance criterion that the entire code sample be covered with types.

  • Migrated the implementation from index.js to index.ts with strict typing on Observer, Observable, request handling, and all mock data
  • Introduced a modular types/ directory with domain-specific type files and a barrel export via types/index.ts
  • Added ObserverLike and ObservableLike contracts so both classes explicitly implement the observer pattern interface
  • Added tsconfig.json with strict: true for compile-time safety

Changes

Core implementation (index.ts)

  • Observer implements ObserverLike with typed next, error, complete, and unsubscribe methods
  • Observable implements ObservableLike with a typed SubscribeFn constructor and subscribe method
  • HTTP constants, userMock, requestsMock, and all handler functions are explicitly typed
  • Handler return types aligned with actual behavior via HandlerResponse

Type definitions (types/)

File Types
http.ts HTTP_METHOD, HTTP_STATUS
user.ts UserRole, User
request.ts PostRequest, GetRequest, Request, RequestError, HandlerResponse
handlers.ts NextHandler, ErrorHandler, CompleteHandler, Handlers
subscription.ts Unsubscribe, Subscription
observer.ts ObserverLike
observable.ts SubscribeFn, ObservableLike, ObservableFactory
index.ts Re-exports all public types

Notable typing decisions

  • Discriminated request unionPostRequest requires a typed User body; GetRequest requires params, preventing invalid request shapes at compile time
  • RequestError — structured error type replacing any
  • UserRole — narrowed to 'user' | 'admin' instead of generic string[]
  • Optional _unsubscribe — satisfies strictPropertyInitialization without weakening type safety

Tooling

  • Added tsconfig.json with strict mode enabled
  • Removed legacy index.js in favor of the typed implementation

Test plan

  • Run npx tsc --noEmit — compiles with zero errors under strict mode
  • Verify no TypeScript/linter diagnostics in index.ts and types/
  • Confirm Observer and Observable satisfy their *Like interfaces
  • Confirm requestsMock type-checks as Request[] (POST with User body, GET with params)
  • Confirm handlers return HandlerResponse with valid HTTP_STATUS values
  • Run node index.ts (or equivalent) and verify complete / unsubscribed log output

Acceptance criteria

  • Observer and Observable are fully typed
  • Request objects are fully typed
  • User objects are fully typed
  • Observer handlers are fully typed
  • Entire code sample is covered with types (no any)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant