Skip to content

Commit dd8a54f

Browse files
committed
refactor(types): stricter ops
1 parent c4ab275 commit dd8a54f

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"lint:fix": "pnpm run lint --write",
3838
"test:types": "tsc --build tsconfig.json",
3939
"test:unit": "vitest --coverage",
40-
"firebase:emulators": "firebase emulators:start --only firestore",
40+
"firebase:emulators": "firebase emulators:start",
4141
"test:dev": "vitest",
4242
"test": "pnpm run lint && pnpm run test:unit run && pnpm run build"
4343
},

src/shared.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import type { DocumentData, DocumentReference } from 'firebase/firestore'
22

3+
// FIXME: replace any with unknown or T generics
4+
35
export interface OperationsType {
4-
set: (target: Record<string, any>, key: string | number, value: any) => any
5-
add: (array: any[], index: number, data: DocumentData) => any
6-
remove: (array: any[], index: number) => any
6+
set<T extends object = Record<any, unknown>>(
7+
target: T,
8+
// accepts a dot delimited path
9+
path: string | number,
10+
value: T[any]
11+
): T[any] | T[any][]
12+
add<T extends unknown = unknown>(array: T[], index: number, data: T): T[]
13+
remove<T extends unknown = unknown>(array: T[], index: number): T[]
714
}
815

916
export type ResetOption = boolean | (() => any)
@@ -28,13 +35,13 @@ export function walkGet(obj: Record<string, any>, path: string): any {
2835
* @param value
2936
* @returns an array with the element that was replaced or the value that was set
3037
*/
31-
export function walkSet<T>(
32-
obj: Record<string, any>,
38+
export function walkSet<T extends object = Record<any, unknown>>(
39+
obj: T,
3340
path: string | number,
34-
value: T
35-
): T | T[] {
41+
value: T[any]
42+
): T[any] | T[any][] {
3643
// path can be a number
37-
const keys = ('' + path).split('.')
44+
const keys = ('' + path).split('.') as Array<keyof T>
3845
const key = keys.pop() as string // split will produce at least one element array
3946
const target = keys.reduce(
4047
(target, key): any =>

tests/firestore.bind.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
getFirestore,
1111
updateDoc,
1212
collection,
13-
get,
1413
deleteDoc,
1514
getDocs,
1615
setDoc,

0 commit comments

Comments
 (0)