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
Open
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
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 @@ -165,8 +165,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 @@ -175,7 +175,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 }),
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

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 @@ -90,7 +90,9 @@ export function useTransactions({
}
},
onError,
options: { pageCount: optionsRef.current.pageCount },
options: optionsRef.current.pageCount
? { pageCount: optionsRef.current.pageCount }
: {},
});

return () => {
Expand Down Expand Up @@ -121,7 +123,7 @@ export function useTransactions({
return {
transactions,
isLoading,
error,
...(error && { error }),
reload,
loadMore,
isLoadingMore,
Expand Down Expand Up @@ -275,10 +277,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 @@ -250,9 +250,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 @@ -261,7 +264,10 @@ export function updateTransaction(
return makeChild(parent, child);
});

return recalculateSplit({ ...parent, subtransactions: sub });
return recalculateSplit({
...parent,
...(sub && { subtransactions: sub }),
});
} else {
return transaction;
}
Expand All @@ -284,7 +290,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 @@ -190,7 +190,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 @@ -226,7 +226,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 @@ -383,8 +383,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 @@ -397,8 +397,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`
Loading