Skip to content

Commit 1dc6da7

Browse files
committed
fix firestore types and add test
1 parent 90ef814 commit 1dc6da7

File tree

9 files changed

+229
-145
lines changed

9 files changed

+229
-145
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"lint:spellcheck": "spellchecker --quiet --files=\"docs/**/*.md\" --dictionaries=\"./.spellcheck.dict.txt\" --reports=\"spelling.json\" --plugins spell indefinite-article repeated-words syntax-mentions syntax-urls frontmatter",
2020
"tsc:compile": "tsc --project .",
2121
"lint:all": "yarn lint && yarn lint:markdown && yarn lint:spellcheck && yarn tsc:compile",
22+
"tests:tsd": "npx tsd packages/firestore",
2223
"tests:ai:mocks": "yarn ts-node ./scripts/fetch_ai_mock_responses.ts && yarn ts-node ./packages/ai/__tests__/test-utils/convert-mocks.ts",
2324
"tests:jest": "jest",
2425
"tests:jest-watch": "jest --watch",
@@ -100,6 +101,7 @@
100101
"spellchecker-cli": "^7.0.0",
101102
"ts-jest": "^29.4.1",
102103
"ts-node": "^10.9.2",
104+
"tsd": "^0.33.0",
103105
"typescript": "^5.9.2"
104106
},
105107
"resolutions": {

packages/app/lib/modular/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,6 @@ export function preferencesSetString(key: string, value: string): Promise<void>;
128128
*
129129
* This is required if you want to persist things like Auth sessions, Analytics device IDs, etc.
130130
*/
131-
export function setReactNativeAsyncStorage(asyncStorage: ReactNativeAsyncStorage): void;
131+
export function setReactNativeAsyncStorage(
132+
asyncStorage: ReactNativeFirebase.ReactNativeAsyncStorage,
133+
): void;

packages/firestore/lib/index.d.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export namespace FirebaseFirestoreTypes {
5656
export type QueryFilterType = 'OR' | 'AND';
5757

5858
export interface QueryFieldFilterConstraint {
59-
fieldPath: keyof T | FieldPath;
59+
fieldPath: FieldPath;
6060
operator: WhereFilterOp;
6161
value: any;
6262
}
@@ -77,7 +77,7 @@ export namespace FirebaseFirestoreTypes {
7777
* e.g. Filter('name', '==', 'Ada')
7878
*/
7979
(
80-
fieldPath: keyof T | FieldPath,
80+
fieldPath: FieldPath | string,
8181
operator: WhereFilterOp,
8282
value: any,
8383
): QueryFieldFilterConstraint;
@@ -894,7 +894,7 @@ export namespace FirebaseFirestoreTypes {
894894
// eslint-disable-next-line @typescript-eslint/no-unused-vars
895895
export class AggregateField<T> {
896896
/** A type string to uniquely identify instances of this class. */
897-
type = 'AggregateField';
897+
readonly type: 'AggregateField';
898898
}
899899

900900
/**
@@ -923,14 +923,13 @@ export namespace FirebaseFirestoreTypes {
923923
*/
924924
export interface AggregateQuerySnapshot<
925925
AggregateSpecType extends AggregateSpec,
926-
AppModelType = DocumentData,
927-
DbModelType extends DocumentData = DocumentData,
926+
T extends DocumentData = DocumentData,
928927
> {
929928
/**
930929
* The underlying query over which the aggregations recorded in this
931930
* `AggregateQuerySnapshot` were performed.
932931
*/
933-
get query(): Query<AppModelType, DbModelType>;
932+
get query(): Query<T>;
934933

935934
/**
936935
* Returns the results of the aggregations performed over the underlying
@@ -953,7 +952,7 @@ export namespace FirebaseFirestoreTypes {
953952
/**
954953
* The underlying query for this instance.
955954
*/
956-
get query(): Query<unknown>;
955+
get query(): Query;
957956

958957
/**
959958
* Executes the query and returns the results as a AggregateQuerySnapshot.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expectType } from 'tsd';
2+
import firebase, {
3+
collection,
4+
doc,
5+
FirebaseFirestoreTypes,
6+
getDoc,
7+
increment,
8+
onSnapshot,
9+
query,
10+
serverTimestamp,
11+
setDoc,
12+
Timestamp,
13+
updateDoc,
14+
where,
15+
} from '.';
16+
17+
export const tests = [
18+
async () => {
19+
type DocShape = { name: string; age: number; createdAt: Timestamp };
20+
const usersCollection = collection<DocShape>(firebase(), 'collectionName');
21+
expectType<FirebaseFirestoreTypes.CollectionReference<DocShape>>(usersCollection);
22+
const userDoc = doc(usersCollection, 'doc-id');
23+
expectType<FirebaseFirestoreTypes.DocumentReference<DocShape>>(userDoc);
24+
const snapshot = await getDoc(userDoc);
25+
expectType<FirebaseFirestoreTypes.DocumentSnapshot<DocShape>>(snapshot);
26+
expectType<DocShape | undefined>(snapshot.data());
27+
28+
updateDoc(userDoc, { age: 20, createdAt: serverTimestamp() });
29+
updateDoc(userDoc, { age: increment(1), createdAt: serverTimestamp() });
30+
31+
setDoc(userDoc, {
32+
name: 'hi',
33+
age: 30,
34+
createdAt: Timestamp.fromDate(new Date()),
35+
});
36+
37+
const q = query(usersCollection, where('not-typed-intentionally', '==', 'CA'));
38+
onSnapshot(q, s => {
39+
for (const doc of s.docs) {
40+
expectType<DocShape>(doc.data());
41+
}
42+
});
43+
44+
onSnapshot(q, {
45+
next: s => {
46+
for (const doc of s.docs) {
47+
expectType<DocShape>(doc.data());
48+
}
49+
},
50+
error: e => {
51+
expectType<Error>(e);
52+
},
53+
});
54+
55+
usersCollection.where('age', '>', 18).onSnapshot(s => {
56+
for (const doc of s.docs) {
57+
expectType<DocShape>(doc.data());
58+
}
59+
});
60+
61+
where('a', '!=', 3);
62+
where('a', '<', 3);
63+
where('a', '<=', 3);
64+
where('a', '==', 3);
65+
where('a', '>', 3);
66+
where('a', '>=', 3);
67+
where('a', 'array-contains', 3);
68+
where('a', 'array-contains-any', 3);
69+
where('a', 'in', 3);
70+
where('a', 'not-in', 3);
71+
},
72+
];

packages/firestore/lib/modular/Bytes.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export declare class Bytes extends FirestoreBlob {
1+
import { FirebaseFirestoreTypes } from '..';
2+
3+
export declare class Bytes extends FirebaseFirestoreTypes.Blob {
24
static fromBase64String(base64: string): Bytes;
35

46
static fromUint8Array(array: Uint8Array): Bytes;

0 commit comments

Comments
 (0)