Skip to content
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

[TypeScript] Add types to loot-core app #4155

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/loot-core/src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ type Events = {
'load-budget': { id: string };
};

type UnlistenService = () => void;
type Service = () => UnlistenService;

class App<Handlers> {
events: Emitter<Events>;
handlers: Handlers;
services;
unlistenServices;
services: Service[];
unlistenServices: UnlistenService[];

constructor() {
this.handlers = {} as Handlers;
this.services = [];
this.events = mitt();
this.events = mitt<Events>();
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Type safety gap in Events interface

The Events type doesn't include the 'change' event emitted in spreadsheet.ts, which bypasses TypeScript's type checking. Additionally, Events only covers a subset of available server events.

  • Add the 'change' event type to Events interface:
type Events = {
  sync: ServerEvents['sync-event'];
  'load-budget': { id: string };
  'change': { names: string[] };  // Add this
};
🔗 Analysis chain

Verify event type coverage.

The generic type parameter properly constrains event types. However, let's verify that all possible events are covered in the Events type.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for event emissions to ensure all are typed
rg -t typescript "this\.events\.emit\(" packages/loot-core/src/

Length of output: 100


Script:

#!/bin/bash
# Search for event emissions without file type restriction
rg "this\.events\.emit\(" packages/loot-core/src/

# Find Events type definition
ast-grep --pattern 'type Events = {
  $$$
}'

# Backup pattern search for Events interface/type
rg -A 10 "^(type|interface)\s+Events\s*=" packages/loot-core/src/

Length of output: 1176


Script:

#!/bin/bash
# Find ServerEvents type definition
rg -A 10 "^(type|interface)\s+ServerEvents\s*=" packages/loot-core/src/

# Find any event listeners for 'change' event
rg "this\.events\.on\('change'" packages/loot-core/src/

# Find any imports of ServerEvents
rg "ServerEvents" packages/loot-core/src/

Length of output: 1077


Script:

#!/bin/bash
# View ServerEvents definition
cat packages/loot-core/src/types/server-events.d.ts

Length of output: 2081

this.unlistenServices = [];
}

Expand All @@ -39,7 +42,7 @@ class App<Handlers> {
this.handlers[name] = func;
}

service(func) {
service(func: Service) {
this.services.push(func);
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4155.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [joel-jeremy]
---

Add types to loot-core app
Loading