Reputation: 8108
I am trying to put the below function inside a .ts file:
function incrementCounter(db, ref, num_shards) {
// Select a shard of the counter at random
const shard_id = Math.floor(Math.random() * num_shards).toString();
const shard_ref = ref.collection('shards').doc(shard_id);
// Update count
return shard_ref.update("count", firebase.firestore.FieldValue.increment(1));
}
This function is from the official firestore documentation: https://firebase.google.com/docs/firestore/solutions/counters
But I am getting this error:
Property 'increment' does not exist on type 'typeof FieldValue'.ts(2339)
Am I supposed to add some type definitions to my project? I am not familiar with ts so I appreciate any help.
p.s. My react-native project still seems to compile and deploy but I want to get rid of this error.
edit:
tsconfig.json -> I don't have this config file in my project.
npm ls | grep firestore
@firebase/[email protected]
│ │ ├── @firebase/[email protected]
import statements:
import React from 'react';
import * as firebase from 'firebase';
import 'firebase/firestore';
Upvotes: 1
Views: 2057
Reputation: 1342
Assuming you are using this npm module which appears to have its source code here, we can take a look at what is happening:
(1) In the index.ts
file of the above repo, we can see the top level type definitions. The first thing that I notice is that they are using the old Typescript module
syntax (note the Copyright date of 2017). Fortunately, that shouldn't be the cause of your issue and we can see in the included interface that FieldValue
is defined and has a type of type.FieldValue
. Notice that types
is included from @firebase/firestore-types
.
(2) Assuming that that module has its source code here, we can see in the index.d.ts
file that FieldValue is a class with increment
attached. This is odd because your error says that the increment
is not included.
(3) Since the types are not actually in the @firebase/firestore
npm module, it is possible that there is a module dependency issue. However, this is not the case as the package.json
specifies the type module correctly as a dependency and uses the latest version. Additionally, they are using the latest version of Typescript so there shouldn't be an issue if you are too.
Based on the above analysis, I think we can safely say that there must be some quirk in your particular setup. Notably, you should NOT have to do anything other than include the relevant node_modules to get this to work. Here are some additional steps that will help with debugging. Please report back with your results:
(1) What versions of Typescript are you using for this project? Can you post your tsconfig.json
as well as the command you are using for compilation (if using a larger bundler utility, let's first try to get it to work without the bundler).
(2) What version of @firebase/firestore
are you using? Please use npm ls
to uncover.
(3) Can you post the import
statements that you are using at the top of your file?
So the output from your npm ls @firebase/firestore
is pretty telling. You are using an older when the documentation you linked to assumes that you are using the latest code. Depending on how much the apis changed over time and how far you are in your project, you can take one of several paths:
(1) Upgrade to a version of firestore that is compatible with the docs. Unfortunately, it isn't clear how the npm versions correlate to the documentation; your best bet is to run npm install --save @firebase/firestore@latest
to make sure you are in line with the current docs
(2) You can try to find the documentation that corresponds to your version of firestore; I couldn't find anything useful on a quick search and I wouldn't recommend this uphill battle if you can avoid it
(3) You can ignore the error with a good old // @ts-ignore
- source
Upvotes: 1