Type errors on updateOne/upsert when schema contains array
Closed this issue · 2 comments
If I create a schema that contains an array, I get an error when trying to use $set within updateOne or upsert. See below to reproduce.
const clubhouseSchema = schema(
{
location: types.string({ required: true }),
mapName: types.string({ required: true }),
title: types.string({ required: true }),
contractName: types.string({ required: true }),
contractAddress: types.string({ required: true }),
traits: types.array(types.number),
},
{
timestamps: true,
},
);
export type ClubhouseDoc = (typeof clubhouseSchema)[0];
const Clubhouse = papr.model('clubhouses', clubhouseSchema);
export default Clubhouse;
const result = await Clubhouse.upsert(
{
mapName: clubhouse.mapName,
},
{
$set: {
location: clubhouse.location,
},
},
);
I get the following error on $set:
TS2322: Type '{ location: string; }' is not assignable to type 'PaprMatchKeysAndValues<{ createdAt: Date; updatedAt: Date; _id: ObjectId; location: string; mapName: string; title: string; contractName: string; contractAddress: string; traits?: ((options?: Options | undefined) => GetType<...>)[] | undefined; }>'. Type '{ location: string; }' is not assignable to type 'PaprArrayElementsProperties<{ createdAt: Date; updatedAt: Date; _id: ObjectId; location: string; mapName: string; title: string; contractName: string; contractAddress: string; traits?: ((options?: Options | undefined) => GetType<...>)[] | undefined; }>'. Property 'location' is incompatible with index signature. Type 'string' is not assignable to type 'undefined'.
mongodbTypes.d.ts(143, 5): The expected type comes from property '$set' which is declared here on type 'PaprUpdateFilter<{ createdAt: Date; updatedAt: Date; _id: ObjectId; location: string; mapName: string; title: string; contractName: string; contractAddress: string; traits?: ((options?: Options | undefined) => GetType<...>)[] | undefined; }>'
If I delete the traits array from the schema, the error goes away.
I can't reproduce that error with the code you posted. I suspect it may be related to your TS version or tsconfig.json
options. Can you please share those?
Ah, you are right. I am currently on Typescript 4.2.4. Upgrading to 4.9.5 removes the error. Thanks!