-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 4.11.0
-
Component/s: TypeScript
What problem are you facing?
I am experiencing typing issues when trying to update records in the database that include a number (array index). The typings show that it is supported, but when actually typing out the update it fails. The error is:
'string' and '`headquarters.$[${string}]`' index signatures are incompatible. Type 'string' is not assignable to type 'Headquarters'.(2322)
Here is a basic example:
Basic Gist:
interface Client { _id: string; headquarters: Headquarters[]; } interface Headquarters { country: string; } const hqCounter = 2; const client = { _id: 'acb123', }; await clients.findOneAndUpdate( { _id: client._id }, { $set: { [`headquarters.${hqCounter}.country`]: 'US' } }, // Type Error Here );
What driver and relevant dependency versions are you using?
This was experienced when we upgraded to v4, and currently we are on 4.11.0 and TypeScript 4.7.4.
Steps to reproduce?
I've put together a stackblitz that allows you to see the intellisense and highlights the type errors: https://stackblitz.com/edit/mongodb-updatefilter-issues?file=index.ts
It appears from the error that it is expecting a different value type based on the key, so I believe there is something incorrect there. I've used my own "object to string path" types elsewhere in my project and it can handle this case just fine (I've added it to the bottom of the stackblitz example).