Skip to content

Open source personal finance application

refactor: fix root causes of 'as unknown' casts

Ilya Komichev7a44a7d00d39
diff --git a/app/components/categories/useCategoriesStore.ts b/app/components/categories/useCategoriesStore.ts
index da26854c..701788be 100644
--- a/app/components/categories/useCategoriesStore.ts
+++ b/app/components/categories/useCategoriesStore.ts
@@ -249,7 +249,7 @@ export const useCategoriesStore = defineStore('categories', (): CategoriesStore
// Optimistic UI
applyOptimisticUpdate(id, values, isUpdateChildCategoriesColor, categoryChildIds)
- if (!pushSaveOp({ entity: 'categories', id, isDemo: !!isDemo.value, isExisting: !!isExisting, values: values as unknown as Record<string, unknown> }))
+ if (!pushSaveOp({ entity: 'categories', id, isDemo: !!isDemo.value, isExisting: !!isExisting, values }))
return
const { api, client } = useConvexClientWithApi()
@@ -315,7 +315,7 @@ export const useCategoriesStore = defineStore('categories', (): CategoriesStore
}
return {
- cancelPersist: () => (debouncedPersist as unknown as { cancel?: () => void }).cancel?.(),
+ cancelPersist: () => debouncedPersist.cancel(),
categoriesForBeParent,
categoriesIdsForTrnValues,
categoriesRootIds,
diff --git a/app/components/offline/helpers.ts b/app/components/offline/helpers.ts
index 84746a79..d3db3b22 100644
--- a/app/components/offline/helpers.ts
+++ b/app/components/offline/helpers.ts
@@ -1,6 +1,6 @@
import localforage from 'localforage'
-import type { EntityType, OfflineOp } from '~/components/offline/types'
+import type { EntityType, OfflineOp, OfflineOpInput } from '~/components/offline/types'
import { createLogger } from '~/utils/logger'
@@ -63,12 +63,12 @@ async function writeQueue(queue: OfflineOp[]): Promise<void> {
*/
let writeChain = Promise.resolve()
-export function pushOfflineOp(op: Omit<OfflineOp, 'timestamp'>): Promise<void> {
+export function pushOfflineOp(op: OfflineOpInput): Promise<void> {
writeChain = writeChain.then(() => pushOfflineOpImpl(op), () => pushOfflineOpImpl(op))
return writeChain
}
-async function pushOfflineOpImpl(op: Omit<OfflineOp, 'timestamp'>): Promise<void> {
+async function pushOfflineOpImpl(op: OfflineOpInput): Promise<void> {
// Ensure queue ownership is stamped on first push
if (_currentUserId) {
const storedUserId = await localforage.getItem<string>(QUEUE_USER_KEY)
diff --git a/app/components/offline/types.ts b/app/components/offline/types.ts
index f4982e0d..4bca87fc 100644
--- a/app/components/offline/types.ts
+++ b/app/components/offline/types.ts
@@ -1,9 +1,16 @@
export type EntityType = 'categories' | 'trns' | 'userSettings' | 'wallets'
+export type OfflineOpData = Record<string, unknown>
+
export type OfflineOp = {
- data?: Record<string, unknown>
+ data?: OfflineOpData
entity: EntityType
id: string
timestamp: number
type: 'create' | 'delete' | 'update'
}
+
+/** Accepted input for pushOfflineOp — allows typed domain objects */
+export type OfflineOpInput = Omit<OfflineOp, 'data' | 'timestamp'> & {
+ data?: Record<string, unknown>
+}
diff --git a/app/components/trns/useTrnsStore.ts b/app/components/trns/useTrnsStore.ts
index bcae1583..3b3b9516 100644
--- a/app/components/trns/useTrnsStore.ts
+++ b/app/components/trns/useTrnsStore.ts
@@ -246,7 +246,7 @@ export const useTrnsStore = defineStore('trns', () => {
// Optimistic UI
setTrns({ ...(items.value ?? {}), [id]: valuesWithEditDate })
- if (!pushSaveOp({ entity: 'trns', id, isDemo: !!isDemo.value, isExisting: !!isExisting, values: values as unknown as Record<string, unknown> }))
+ if (!pushSaveOp({ entity: 'trns', id, isDemo: !!isDemo.value, isExisting: !!isExisting, values }))
return
const { api, client } = useConvexClientWithApi()
@@ -392,7 +392,7 @@ export const useTrnsStore = defineStore('trns', () => {
}
return {
- cancelPersist: () => (debouncedPersist as unknown as { cancel?: () => void }).cancel?.(),
+ cancelPersist: () => debouncedPersist.cancel(),
computeTrnItem,
deleteTrn,
getRange,
diff --git a/app/components/wallets/page/New.vue b/app/components/wallets/page/New.vue
index 88fff828..406701d8 100644
--- a/app/components/wallets/page/New.vue
+++ b/app/components/wallets/page/New.vue
@@ -10,6 +10,10 @@ const walletForm = ref(walletItemSchema.parse({ type: 'cash' }))
const isOnboarding = computed(() => 'onboarding' in route.query)
+function updateField(key: keyof WalletItem, value: WalletItem[keyof WalletItem]) {
+ (walletForm.value as Record<string, unknown>)[key] = value
+}
+
useHead({
title: `${t('base.add')}: ${walletForm.value.name ? walletForm.value.name : t('wallets.form.name.label')}`,
})
@@ -33,7 +37,7 @@ useHead({
<WalletsForm
:walletForm="walletForm"
@afterSave="() => router.replace('/dashboard')"
- @update="(id: keyof WalletItem, value: WalletItem[keyof WalletItem]) => (walletForm as unknown as Record<keyof WalletItem, WalletItem[keyof WalletItem]>)[id] = value"
+ @update="updateField"
/>
</UiPage>
</template>
diff --git a/app/components/wallets/page/id/Edit.vue b/app/components/wallets/page/id/Edit.vue
index 8e2b4a21..cddec9f8 100644
--- a/app/components/wallets/page/id/Edit.vue
+++ b/app/components/wallets/page/id/Edit.vue
@@ -13,6 +13,10 @@ const walletId = computed(() => route.params.id) as Ref<WalletId>
const wallet = computed(() => walletsStore.items?.[walletId.value])
const walletForm = ref(walletItemSchema.parse(wallet.value))
+function updateField(key: keyof WalletItem, value: WalletItem[keyof WalletItem]) {
+ (walletForm.value as Record<string, unknown>)[key] = value
+}
+
useHead({ title: `${t('base.edit')}: ${walletForm.value?.name || t('wallets.form.name.label')}` })
</script>
@@ -32,7 +36,7 @@ useHead({ title: `${t('base.edit')}: ${walletForm.value?.name || t('wallets.form
:walletId
:walletForm
@afterSave="() => router.replace(`/wallets/${walletId}`)"
- @update="(key: keyof WalletItem, value: WalletItem[keyof WalletItem]) => (walletForm as unknown as Record<keyof WalletItem, WalletItem[keyof WalletItem]>)[key] = value"
+ @update="updateField"
/>
</UiPage>
</template>
diff --git a/app/components/wallets/useWalletsStore.ts b/app/components/wallets/useWalletsStore.ts
index 180c9da6..4a60c9ee 100644
--- a/app/components/wallets/useWalletsStore.ts
+++ b/app/components/wallets/useWalletsStore.ts
@@ -73,7 +73,7 @@ export const useWalletsStore = defineStore('wallets', () => {
[id]: values,
})
- if (!pushSaveOp({ entity: 'wallets', id, isDemo: !!isDemo.value, isExisting: !!isExisting, values: values as unknown as Record<string, unknown> }))
+ if (!pushSaveOp({ entity: 'wallets', id, isDemo: !!isDemo.value, isExisting: !!isExisting, values }))
return
const { api, client } = useConvexClientWithApi()
@@ -126,7 +126,7 @@ export const useWalletsStore = defineStore('wallets', () => {
if (!isReplaying()) {
for (const id of ids) {
if (updated[id])
- pushOfflineOp({ data: updated[id] as unknown as Record<string, unknown>, entity: 'wallets', id, type: 'update' })
+ pushOfflineOp({ data: updated[id], entity: 'wallets', id, type: 'update' })
}
}
@@ -213,7 +213,7 @@ export const useWalletsStore = defineStore('wallets', () => {
}
return {
- cancelPersist: () => (debouncedPersist as unknown as { cancel?: () => void }).cancel?.(),
+ cancelPersist: () => debouncedPersist.cancel(),
currenciesUsed,
deleteWallet,
hasItems,
diff --git a/app/composables/useStoreSync.ts b/app/composables/useStoreSync.ts
index cffa1e0f..25cfc7ef 100644
--- a/app/composables/useStoreSync.ts
+++ b/app/composables/useStoreSync.ts
@@ -31,20 +31,28 @@ export function isPersistBlocked(): boolean {
return _persistBlocked
}
-export function createDebouncedPersist<T>(storageKey: string) {
- return useDebounceFn((values: T) => {
+export type DebouncedPersist<T> = {
+ (values: T): Promise<void>
+ cancel: () => void
+}
+
+export function createDebouncedPersist<T>(storageKey: string): DebouncedPersist<T> {
+ const fn = useDebounceFn((values: T) => {
if (_persistBlocked)
return
localforage.setItem(storageKey, values)
}, 300)
+
+ // useDebounceFn has .cancel() at runtime but doesn't expose it in types
+ return fn as unknown as DebouncedPersist<T>
}
-export function pushSaveOp(opts: {
+export function pushSaveOp<T extends Record<string, unknown>>(opts: {
entity: EntityType
id: string
isDemo: boolean
isExisting: boolean
- values: Record<string, unknown>
+ values: T
}): boolean {
if (opts.isDemo)
return false