googleapis / googleapis/google-cloud-node

Updating a nested document with a dynamic path using typescript

Geschlossen
#7,464 11 Kommentare 1 Reaktion 1 zugewiesene Person Zugewiesen an @MarkDuckworth Auf GitHub ansehen
api: firestore library: nodejs-firestore priority: p3 type: question
Vorherrschende Sprache
TypeScript
Sterne
3.2k
Forks
712
Ø Merge
2 T. 9 Std.
Gemergte PRs (30 T.)
104

Beschreibung

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:
```typescript
// no type error
const keyPath = "paymentHistory.test";
t.update(userRef, {
[keyPath]: payment,
});
```

But strangely this results in a complex type error involving `AddPrefixToKeys`.
```typescript
// type error
const keyPath = "paymentHistory." + "test";
t.update(userRef, {
[keyPath]: payment,
});
```

The `paymentHistory` property on the User object is defined like this:
```typescript
paymentHistory: Record
```

I am using package versions:
```
"firebase-admin": "11.10.1",
"firebase-functions": "4.4.1",
"typescript": "5.2.2"
```

How can I satisfy the types, when the nested key `"test"` is a variable and not known at build time?

Thanks!

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.