-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating a nested document with a dynamic path using typescript #1890
Comments
Hi @PWhiddy, thank you for reporting this issue. Could you please provide a full repro of what you are updating, ie, what is the type of t, and userRef? I am suspecting this might be related to this firebase/firebase-js-sdk#5853 |
Hi! import {firestore} from "firebase-admin";
import {CollectionReference} from "firebase-admin/firestore";
interface MyDoc {
nestedA: Record<string, number>
// The issue goes away if nestedB is removed
nestedB: Record<string, string>
}
const fstore = firestore();
// The type error goes away if the collection
// is not asserted as "MyDoc", but then type safety is lost
const docStore = fstore.collection("all_docs") as CollectionReference<MyDoc>;
const docRef = docStore.doc("test_doc");
const goodKey = "nestedA.test";
const badKey = "nestedA." + "test";
// works fine
fstore.runTransaction(async (t) => {
t.update(docRef, {
[goodKey]: 3,
});
});
// type error
fstore.runTransaction(async (t) => {
t.update(docRef, {
[badKey]: 3,
});
});
{
"name": "test",
"version": "0.0.1",
"scripts": {
"build": "tsc"
},
"main": "index.ts",
"dependencies": {
"firebase-admin": "^11.10.1"
},
"devDependencies": {
"typescript": "^5.2.2"
}
}
{
"compilerOptions": {
"module": "commonjs",
"outDir": "lib",
"strict": true,
"target": "ES2022",
},
"include": [
"src/**/*"
],
"rootDirs": ["./src"]
}
Really I need the nested key "test" to be dynamic, as is allowed by the type definition of MyDoc, but this simpler case of concatenated string literals makes the strangeness more obvious. This code built fine using an earlier version of typescript/firestore from late last year. Here is a running demo of these files: |
Hi @PWhiddy, we have recently fixed a similar issue in the firebase-js-sdk: firebase/firebase-js-sdk#7318. We are planning to port the changes to nodejs-firestore, but I cannot provide an estimated timeline for it. This is added to our backlog along with other tasks. Rest assured, I'll keep you posted on any updates to this thread. Google internal users please see b/298394991. |
Internally tracked by b/311751201. |
Any update on this? it's stopping us upgrading past |
Any update on this? |
Hey! Just wanted to know if there's any update on this. |
Can we get an update on this? This is urgent. |
I have some code which was working great with earlier versions of firestore/typescript, but after updating can't see how firestore expects nested objects to be updated with dynamic paths.
It happily accepts a single string literal like this:
But strangely this results in a complex type error involving
AddPrefixToKeys
.The
paymentHistory
property on the User object is defined like this:I am using package versions:
How can I satisfy the types, when the nested key
"test"
is a variable and not known at build time?Thanks!
The text was updated successfully, but these errors were encountered: