Skip to content

Commit

Permalink
Make loot-core compatible with exactOptionalPropertyTypes (#4214)
Browse files Browse the repository at this point in the history
* Make `loot-core` compatible with `exactOptionalPropertyTypes`

* Add release notes
  • Loading branch information
jfdoming authored Feb 12, 2025
1 parent f1a4c88 commit 454f019
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/loot-core/src/client/accounts/accountsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function handleSyncResponse(
}

type SyncAccountsPayload = {
id?: AccountEntity['id'];
id?: AccountEntity['id'] | undefined;
};

export const syncAccounts = createAppAsyncThunk(
Expand Down
6 changes: 3 additions & 3 deletions packages/loot-core/src/client/actions/budgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ export function duplicateBudget({
loadBudget = 'none',
cloudSync,
}: {
id?: string;
cloudId?: string;
id?: string | undefined;
cloudId?: string | undefined;
oldName: string;
newName: string;
managePage?: boolean;
Expand All @@ -178,7 +178,7 @@ export function duplicateBudget({
* cloudSync is used to determine if the duplicate budget
* should be synced to the server
*/
cloudSync?: boolean;
cloudSync: boolean;
}) {
return async (dispatch: AppDispatch) => {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/loot-core/src/client/data-hooks/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
conditionsOp: row.conditions_op ?? 'and',
data: row.metadata,
...(row.metadata && { metadata: row.metadata }),
};
return report;
});
Expand Down
9 changes: 6 additions & 3 deletions packages/loot-core/src/client/data-hooks/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export function useTransactions({
}
},
onError,
options: { pageCount: optionsRef.current.pageCount },
options: optionsRef.current.pageCount
? { pageCount: optionsRef.current.pageCount }
: {},
});

return () => {
Expand Down Expand Up @@ -122,7 +124,7 @@ export function useTransactions({
return {
transactions,
isLoading,
error,
...(error && { error }),
reload,
loadMore,
isLoadingMore,
Expand Down Expand Up @@ -274,10 +276,11 @@ export function usePreviewTransactions(): UsePreviewTransactionsResult {
};
}, [scheduleTransactions, schedules, statuses, upcomingLength]);

const returnError = error || scheduleQueryError;
return {
data: previewTransactions,
isLoading: isLoading || isSchedulesLoading,
error: error || scheduleQueryError,
...(returnError && { error: returnError }),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/client/query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ export function useQuery<Response = unknown>(
return {
data,
isLoading,
error,
...(error && { error }),
};
}
4 changes: 2 additions & 2 deletions packages/loot-core/src/client/state-types/modals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type FinanceModals = {
'select-linked-accounts': {
accounts: unknown[];
requisitionId?: string;
upgradingAccountId?: string;
upgradingAccountId?: string | undefined;
syncSource?: AccountSyncSource;
};

Expand Down Expand Up @@ -74,7 +74,7 @@ type FinanceModals = {
onMoveExternal: (arg: {
institutionId: string;
}) => Promise<{ error: string } | { data: unknown }>;
onClose?: () => void;
onClose?: (() => void) | undefined;
onSuccess: (data: GoCardlessToken) => Promise<void>;
};

Expand Down
26 changes: 14 additions & 12 deletions packages/loot-core/src/client/state-types/notifications.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import type * as constants from '../constants';

export type Notification = {
id?: string;
id?: string | undefined;
// 'warning' is unhandled??
type?: 'message' | 'error' | 'warning';
pre?: string;
title?: string;
pre?: string | undefined;
title?: string | undefined;
message: string;
sticky?: boolean;
timeout?: number;
button?: {
title: string;
action: () => void | Promise<void>;
};
messageActions?: Record<string, () => void>;
onClose?: () => void;
internal?: string;
sticky?: boolean | undefined;
timeout?: number | undefined;
button?:
| {
title: string;
action: () => void | Promise<void>;
}
| undefined;
messageActions?: Record<string, () => void> | undefined;
onClose?: (() => void) | undefined;
internal?: string | undefined;
};
type NotificationWithId = Notification & { id: string };

Expand Down
10 changes: 7 additions & 3 deletions packages/loot-core/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ export function generateCategoryGroups(
return definition.map(group => {
const g = generateCategoryGroup(group.name ?? '', group.is_income);

if (!group.categories) {
return g;
}

return {
...g,
categories: group.categories?.map(cat =>
categories: group.categories.map(cat =>
generateCategory(cat.name, g.id, cat.is_income),
),
};
Expand All @@ -117,9 +121,9 @@ function _generateTransaction(
notes: 'Notes',
account: data.account,
date: data.date || monthUtils.currentDay(),
category: data.category,
sort_order: data.sort_order != null ? data.sort_order : 1,
cleared: false,
...(data.category && { category: data.category }),
};
}

Expand Down Expand Up @@ -186,7 +190,7 @@ export function generateTransactions(
{
account: accountId,
category: groupId,
amount: isSplit ? 50 : undefined,
...(isSplit && { amount: 50 }),
sort_order: i,
},
isSplit ? 30 : undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/api-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const categoryModel = {
name: category.name,
is_income: category.is_income ? true : false,
hidden: category.hidden ? true : false,
group_id: category.cat_group,
...(category.cat_group && { group_id: category.cat_group }),
};
},

Expand Down
7 changes: 4 additions & 3 deletions packages/loot-core/src/server/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO: normalize error types
export class PostError extends Error {
meta?: { meta: string };
meta: { meta: string } | undefined;
reason: string;
type: 'PostError';

Expand All @@ -24,14 +24,15 @@ export class HTTPError extends Error {
}

export class SyncError extends Error {
meta?:
meta:
| {
isMissingKey: boolean;
}
| {
error: { message: string; stack: string };
query: { sql: string; params: Array<string | number> };
};
}
| undefined;
reason: string;

constructor(
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/shared/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function resetTracer() {
}

export function execTracer<T>() {
const queue: Array<{ name: string; data?: T }> = [];
const queue: Array<{ name: string; data?: T | undefined }> = [];
let hasStarted = false;
let waitingFor: null | {
name: string;
Expand Down
17 changes: 13 additions & 4 deletions packages/loot-core/src/shared/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,12 @@ export function updateTransaction(

let child = t;
if (trans.id === transaction.id) {
const { payee: childPayee, ...rest } = t;
const newPayee =
childPayee === trans.payee ? transaction.payee : childPayee;
child = {
...t,
payee: t.payee === trans.payee ? transaction.payee : t.payee,
...rest,
...(newPayee != null ? { payee: newPayee } : {}),
};
} else if (t.id === transaction.id) {
child = transaction;
Expand All @@ -260,7 +263,10 @@ export function updateTransaction(
return makeChild(parent, child);
});

return recalculateSplit({ ...parent, subtransactions: sub });
return recalculateSplit({
...parent,
...(sub && { subtransactions: sub }),
});
} else {
return transaction;
}
Expand All @@ -283,7 +289,10 @@ export function deleteTransaction(
} satisfies TransactionEntity;
} else {
const sub = trans.subtransactions?.filter(t => t.id !== id);
return recalculateSplit({ ...trans, subtransactions: sub });
return recalculateSplit({
...trans,
...(sub && { subtransactions: sub }),
});
}
} else {
return null;
Expand Down
12 changes: 6 additions & 6 deletions packages/loot-core/src/types/server-handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export interface ServerHandlers {
'secret-check': (arg: string) => Promise<string | { error?: string }>;

'gocardless-poll-web-token': (arg: {
upgradingAccountId?: string;
upgradingAccountId?: string | undefined;
requisitionId: string;
}) => Promise<
{ error: 'unknown' } | { error: 'timeout' } | { data: GoCardlessToken }
Expand Down Expand Up @@ -228,7 +228,7 @@ export interface ServerHandlers {
'gocardless-poll-web-token-stop': () => Promise<'ok'>;

'gocardless-create-web-token': (arg: {
upgradingAccountId?: string;
upgradingAccountId?: string | undefined;
institutionId: string;
accessValidForDays: number;
}) => Promise<
Expand Down Expand Up @@ -385,8 +385,8 @@ export interface ServerHandlers {
'close-budget': () => Promise<'ok'>;

'delete-budget': (arg: {
id?: string;
cloudFileId?: string;
id?: string | undefined;
cloudFileId?: string | undefined;
}) => Promise<'ok' | 'fail'>;

/**
Expand All @@ -399,8 +399,8 @@ export interface ServerHandlers {
* @returns {Promise<string>} The ID of the newly created budget.
*/
'duplicate-budget': (arg: {
id?: string;
cloudId?: string;
id?: string | undefined;
cloudId?: string | undefined;
newName: string;
cloudSync?: boolean;
open: 'none' | 'original' | 'copy';
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4214.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [jfdoming]
---

Make `loot-core` compatible with `exactOptionalPropertyTypes`

0 comments on commit 454f019

Please sign in to comment.