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

Make loot-core compatible with exactOptionalPropertyTypes #4214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jfdoming
Copy link
Contributor

Part 2 from #4189

@actual-github-bot actual-github-bot bot changed the title Make loot-core compatible with exactOptionalPropertyTypes [WIP] Make loot-core compatible with exactOptionalPropertyTypes Jan 21, 2025
Copy link

netlify bot commented Jan 21, 2025

Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit 115488a
🔍 Latest deploy log https://app.netlify.com/sites/actualbudget/deploys/678f2a7bfb101a0008b5d885
😎 Deploy Preview https://deploy-preview-4214.demo.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Jan 21, 2025

Bundle Stats — desktop-client

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
15 6.69 MB → 6.69 MB (+1.1 kB) +0.02%
Changeset
File Δ Size
home/runner/work/actual/actual/packages/loot-core/src/client/data-hooks/reports.ts 📈 +80 B (+5.48%) 1.43 kB → 1.5 kB
home/runner/work/actual/actual/packages/loot-core/src/client/query-hooks.ts 📈 +26 B (+3.05%) 852 B → 878 B
home/runner/work/actual/actual/packages/loot-core/src/shared/transactions.ts 📈 +223 B (+2.61%) 8.35 kB → 8.57 kB
home/runner/work/actual/actual/packages/loot-core/src/client/data-hooks/transactions.ts 📈 +129 B (+2.08%) 6.05 kB → 6.17 kB
locale/pt-BR.json 📈 +672 B (+0.61%) 108.13 kB → 108.78 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
static/js/pt-BR.js 108.13 kB → 108.78 kB (+672 B) +0.61%
static/js/index.js 4.28 MB → 4.28 MB (+458 B) +0.01%

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
static/js/nl.js 62.88 kB 0%
static/js/en-GB.js 97.97 kB 0%
static/js/en.js 98.94 kB 0%
static/js/indexeddb-main-thread-worker-e59fee74.js 13.5 kB 0%
static/js/workbox-window.prod.es5.js 5.69 kB 0%
static/js/resize-observer.js 18.37 kB 0%
static/js/uk.js 118.39 kB 0%
static/js/BackgroundImage.js 122.29 kB 0%
static/js/useAccountPreviewTransactions.js 1.69 kB 0%
static/js/AppliedFilters.js 10.52 kB 0%
static/js/narrow.js 84.83 kB 0%
static/js/wide.js 101.32 kB 0%
static/js/ReportRouter.js 1.59 MB 0%

Copy link
Contributor

github-actions bot commented Jan 21, 2025

Bundle Stats — loot-core

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
1 1.33 MB → 1.33 MB (+71 B) +0.01%
Changeset
File Δ Size
packages/loot-core/src/shared/transactions.ts 📈 +309 B (+2.92%) 10.35 kB → 10.65 kB
packages/loot-core/src/server/api-models.ts 📈 +57 B (+2.00%) 2.78 kB → 2.84 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
kcab.worker.js 1.33 MB → 1.33 MB (+71 B) +0.01%

Smaller

No assets were smaller

Unchanged

No assets were unchanged

@jfdoming jfdoming changed the title [WIP] Make loot-core compatible with exactOptionalPropertyTypes Make loot-core compatible with exactOptionalPropertyTypes Jan 21, 2025
Copy link
Contributor

coderabbitai bot commented Jan 21, 2025

Walkthrough

This pull request introduces a series of type refinements across multiple files in the loot-core package. The changes primarily focus on explicitly allowing undefined for optional properties in various type definitions and function signatures. These modifications enhance type safety by clarifying how optional properties can be handled, ensuring more precise type checking and reducing potential runtime errors. The modifications span different modules including accounts, budgets, transactions, error handling, and server-side type definitions, demonstrating a systematic approach to improving type consistency throughout the codebase.

Possibly related PRs

Suggested labels

typescript, type-refinement, code-quality

Suggested reviewers

  • joel-jeremy
  • matt-fidd
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/loot-core/src/server/errors.ts (1)

27-35: LGTM! Enhanced type definition for SyncError meta property.

The complex union type with explicit undefined improves type safety while maintaining flexibility for different error scenarios.

Consider adding JSDoc comments to document the different meta shapes and their use cases.

packages/loot-core/src/server/api-models.ts (1)

57-57: LGTM! Consider adding a type assertion for better type safety.

The conditional spread operator ensures group_id is only included when cat_group exists. This aligns well with exactOptionalPropertyTypes.

Consider adding a type assertion to make the relationship between cat_group and group_id more explicit:

-      ...(category.cat_group && { group_id: category.cat_group }),
+      ...(category.cat_group && { group_id: category.cat_group as string }),
packages/loot-core/src/client/actions/budgets.ts (1)

178-178: Update documentation to reflect required parameter.

Since cloudSync is now a required parameter, the documentation should be updated to reflect this change.

   /**
    * cloudSync is used to determine if the duplicate budget
-   * should be synced to the server
+   * should be synced to the server.
+   * @required This parameter must be provided.
    */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7791b74 and 115488a.

⛔ Files ignored due to path filters (1)
  • upcoming-release-notes/4214.md is excluded by !**/*.md
📒 Files selected for processing (13)
  • packages/loot-core/src/client/accounts/accountsSlice.ts (1 hunks)
  • packages/loot-core/src/client/actions/budgets.ts (2 hunks)
  • packages/loot-core/src/client/data-hooks/reports.ts (1 hunks)
  • packages/loot-core/src/client/data-hooks/transactions.ts (3 hunks)
  • packages/loot-core/src/client/query-hooks.ts (1 hunks)
  • packages/loot-core/src/client/state-types/modals.d.ts (2 hunks)
  • packages/loot-core/src/client/state-types/notifications.d.ts (1 hunks)
  • packages/loot-core/src/mocks/index.ts (3 hunks)
  • packages/loot-core/src/server/api-models.ts (1 hunks)
  • packages/loot-core/src/server/errors.ts (2 hunks)
  • packages/loot-core/src/shared/test-helpers.ts (1 hunks)
  • packages/loot-core/src/shared/transactions.ts (3 hunks)
  • packages/loot-core/src/types/server-handlers.d.ts (4 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/loot-core/src/client/accounts/accountsSlice.ts
🧰 Additional context used
📓 Learnings (1)
packages/loot-core/src/client/data-hooks/transactions.ts (1)
Learnt from: joel-jeremy
PR: actualbudget/actual#3685
File: packages/loot-core/src/client/data-hooks/transactions.ts:32-88
Timestamp: 2024-11-10T16:45:25.627Z
Learning: In the React `useTransactions` hook defined in `packages/loot-core/src/client/data-hooks/transactions.ts`, changes to `options` (e.g., `options.pageCount`) should be applied on the next query without adding them to the `useEffect` dependency array, to avoid unnecessary rerenders when options change.
🔇 Additional comments (17)
packages/loot-core/src/client/state-types/notifications.d.ts (2)

4-4: LGTM! Type definitions are now more explicit.

The changes correctly make optional properties more explicit by adding | undefined to their types, which is necessary for compatibility with TypeScript's exactOptionalPropertyTypes flag.

Also applies to: 7-8, 10-20


5-6: Address the TODO comment about unhandled warning type.

The comment indicates that the 'warning' type is unhandled, yet it's included in the type union. This inconsistency should be resolved.

Would you like me to help investigate how warnings are handled in the codebase?

packages/loot-core/src/client/query-hooks.ts (1)

56-56: LGTM! Improved error handling in return type.

The change to conditionally spread the error property (...(error && { error })) is a more precise implementation that aligns with TypeScript's exactOptionalPropertyTypes. This ensures the error property only exists when there's an actual error.

packages/loot-core/src/client/data-hooks/reports.ts (1)

30-32: LGTM! Improved object construction with conditional properties.

The changes improve type safety by:

  1. Only including conditions and metadata when they exist
  2. Using nullish coalescing for conditionsOp to provide a safe default
packages/loot-core/src/server/errors.ts (1)

3-3: LGTM! Improved type safety for PostError meta property.

The change from optional property to required property with explicit undefined improves type safety and aligns with exactOptionalPropertyTypes.

packages/loot-core/src/shared/test-helpers.ts (1)

22-22: LGTM! Type definition aligns with exactOptionalPropertyTypes.

The explicit | undefined in the type definition makes it clear that data can be undefined, which is good for type safety.

packages/loot-core/src/mocks/index.ts (3)

101-104: LGTM! Early return improves code clarity.

The early return when categories is undefined makes the code more maintainable and aligns with exactOptionalPropertyTypes.


126-126: LGTM! Conditional spread operator enhances type safety.

Using the spread operator ensures category is only included when it exists, preventing undefined values from being explicitly set.


193-193: LGTM! Conditional amount handling is more explicit.

The conditional spread for amount makes it clear when the property should be included.

packages/loot-core/src/client/actions/budgets.ts (1)

168-169: LGTM! Explicit optional types enhance type safety.

The explicit | undefined in optional parameter types aligns with exactOptionalPropertyTypes.

packages/loot-core/src/client/data-hooks/transactions.ts (2)

93-95: Improve object construction for better type safety.

The conditional object construction ensures that pageCount is only included when defined, aligning with TypeScript's exactOptionalPropertyTypes.


126-126: Enhance error handling with conditional spreading.

The use of conditional spreading for error objects improves type safety by ensuring error properties are only included when they exist. The consolidation of error handling in usePreviewTransactions reduces code duplication.

Also applies to: 280-280, 284-284

packages/loot-core/src/client/state-types/modals.d.ts (1)

45-45: Improve type definitions for optional properties.

The explicit | undefined union type for optional properties aligns with TypeScript's exactOptionalPropertyTypes flag, making the optionality more precise.

Also applies to: 77-77

packages/loot-core/src/shared/transactions.ts (2)

253-258: Improve payee update logic with destructuring.

The refactored payee update logic is more explicit and type-safe, correctly handling the case where child transactions inherit the parent's payee.


267-270: Enhance type safety with conditional spreading.

The conditional spreading of subtransactions in both updateTransaction and deleteTransaction functions ensures type safety by only including the property when it exists.

Also applies to: 293-296

packages/loot-core/src/types/server-handlers.d.ts (2)

193-193: Improve type definitions for GoCardless handlers.

The explicit | undefined union type for upgradingAccountId in GoCardless handlers aligns with TypeScript's exactOptionalPropertyTypes flag.

Also applies to: 229-229


386-387: Enhance type safety for budget operations.

The explicit | undefined union type for budget identifiers in delete-budget and duplicate-budget handlers improves type safety and aligns with TypeScript's exactOptionalPropertyTypes flag.

Also applies to: 400-401

@@ -27,9 +27,9 @@ function toJS(rows: CustomReportData[]) {
includeCurrentInterval: row.include_current === 1,
showUncategorized: row.show_uncategorized === 1,
graphType: row.graph_type,
conditions: row.conditions,
...(row.conditions && { conditions: row.conditions }),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Would this also work? This form is more readable IMO.

Suggested change
...(row.conditions && { conditions: row.conditions }),
conditions: row.conditions ? row.conditions : undefined,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately no, this rule makes it so you have to either fully omit the key, or allow the type to be undefined. So in this case, we either do the ugly spread thing, or we update CustomReportEntity to allow conditions to be undefined. Ideally I'd like to avoid making our model objects accept undefined, but defer to you

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

Successfully merging this pull request may close these issues.

2 participants