Skip to content

Commit b2eda1f

Browse files
committed
feat(types): complete typings as much as possible
1 parent 712680c commit b2eda1f

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"module": "dist/vuefire.esm.js",
77
"unpkg": "dist/vuefire.js",
88
"browser": "dist/vuefire.esm.js",
9-
"types": "./types/index.d.ts",
9+
"types": "types/index.d.ts",
1010
"files": [
11-
"dist/**",
11+
"dist/*.js",
12+
"src",
13+
"types/*.d.ts",
1214
"LICENSE",
1315
"README.md"
1416
],

types/index.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import * as firebase from 'firebase'
22
import { PluginFunction } from 'vue'
3+
import './vue'
34

4-
export declare const install: PluginFunction<never>
5+
declare const Vuefire: PluginFunction<never>
6+
7+
export default Vuefire

types/test/index.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Vue from 'vue'
2+
import * as firebase from 'firebase'
3+
import Vuefire from '../index'
4+
5+
const db = firebase.firestore()
6+
7+
Vue.use(Vuefire)
8+
9+
const app = new Vue({
10+
firestore: {
11+
todos: db.collection('todos'),
12+
doc: db.collection('todos').doc('2')
13+
}
14+
})
15+
16+
new Vue({
17+
firestore() {
18+
return {
19+
todos: db.collection('todos'),
20+
doc: db.collection('todos').doc('2')
21+
}
22+
}
23+
})
24+
25+
app.$firestoreRefs.todos.id
26+
27+
app.$bind('document', db.collection('todos').doc('1')).then(doc => {
28+
doc.something
29+
})
30+
31+
app.$bind('collection', db.collection('todos')).then(todos => {
32+
todos.length
33+
})
34+
35+
app.$unbind('document')
36+
app.$unbind('collection')
37+
38+
Vue.component('firestore-component', {
39+
firestore: {
40+
todos: db.collection('todos'),
41+
doc: db.collection('todos').doc('2')
42+
},
43+
methods: {
44+
doSomething() {
45+
// TODO not sure if making this work is possible
46+
// this.todos.map(todo => todo.id)
47+
}
48+
}
49+
})

types/test/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es5",
4-
"lib": ["es2015"],
4+
"lib": ["dom", "es2015"],
55
"module": "commonjs",
66
"strict": true,
77
"noEmit": true,
@@ -10,6 +10,6 @@
1010
"vuefire": ["../index.d.ts"]
1111
}
1212
},
13-
"files": ["../index.d.ts", "../vue.d.ts"],
13+
"files": ["index.ts", "../index.d.ts", "../vue.d.ts"],
1414
"compileOnSave": false
1515
}

types/vue.d.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@
33
*/
44

55
import Vue from 'vue'
6+
import { firestore } from 'firebase'
7+
8+
declare function bindCollection(
9+
name: string,
10+
reference: firestore.CollectionReference
11+
): Promise<firestore.DocumentData[]>
12+
13+
declare function bindDocument(
14+
name: string,
15+
reference: firestore.DocumentReference
16+
): Promise<firestore.DocumentData>
617

718
declare module 'vue/types/vue' {
819
interface Vue {
9-
$bind: any
10-
$unbind: any
20+
$bind: typeof bindCollection & typeof bindDocument
21+
$unbind: (name: string) => void
22+
$firestoreRefs: Readonly<
23+
Record<string, firestore.DocumentReference | firestore.CollectionReference>
24+
>
1125
}
1226
}
1327

28+
type VueFirestoreObject = Record<
29+
string,
30+
firestore.DocumentReference | firestore.CollectionReference
31+
>
32+
33+
type FirestoreOption<V> = VueFirestoreObject | ((this: V) => VueFirestoreObject)
34+
1435
declare module 'vue/types/options' {
1536
interface ComponentOptions<V extends Vue> {
16-
firestore?: any
37+
firestore?: FirestoreOption<V>
1738
}
1839
}

0 commit comments

Comments
 (0)