firebase / firebase/firebase-js-sdk
Firestore: UpdateData<T> does not properly type nested dynamic keys
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 37
Description
### Operating System
Windows 11
### Environment (if applicable)
TypeScript v5
### Firebase SDK Version
12.15.0
### Firebase SDK Product(s)
Firestore
### Project Tooling
Next.js v16 app
### Detailed Problem Description
I have a document that has many nested dynamic fields using the `Record` type (e.g. Record>). When wrapping the type of my document with `UpdateData`, the dynamic field has the type `any` instead of the type `SomeType | FieldValue | undefined`, which is what I had expected given `UpdateData`'s behavior when used on static types.
Additionally, this issue was only observed when the `Record` type was nested in other types and not when it was used at the top-level.
### Steps and code to reproduce issue
**Nested Dynamic Keys are inferred incorrectly**
```
type TestDoc = {
dynamicKeys: Record;
staticKeys: {
staticKey: number;
}
}
type UpdateTestDoc = UpdateData;
```
Type inference on `UpdateTestDoc` gives:
```
type UpdateTestDoc = {
[key: `dynamicKeys.${string}`]: any;
dynamicKeys: { ... 1 more } | FieldValue | undefined;
staticKeys: { ... 1 more } | FieldValue | undefined;
"staticKeys.staticKey": number | FieldValue | undefined;
}
```
The types of static nested fields are inferred properly while the dynamic nested field `dynamicKeys.${string}` is not. It has type `any` when the expected would be `string | FieldValue | undefined`.
**Top-Level Dynamic Keys are inferred properly**
This is only an issue when the dynamic key is nested. If it's top-level, it works fine as shown below:
```
type TestDoc = Record;
type UpdateTestDoc = UpdateData;
```
Type inference on `UpdateTestDoc` gives:
```
type UpdateTestDoc = {
[key: string]: string | FieldValue | undefined;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.