Skip to content
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

Record<string, string> as valid query select argument #14371

Merged
merged 1 commit into from Feb 22, 2024

Conversation

sderrow
Copy link
Contributor

@sderrow sderrow commented Feb 22, 2024

Summary

A Record<string, string> is a valid argument to Query.select, but Typescript currently rejects it since it doesn't overlap with string | string[] | Record<string, number | boolean | object>.

Examples

import mongoose from "mongoose";

export const selectTypeTest = async (): Promise<void> => {
  type Test = {
    _id: mongoose.Types.ObjectId;

    testProp: string;

    createdAt: number;
    updatedAt: number;
  };

  const schema = new mongoose.Schema<Test>(
    {
      testProp: { type: String },
      createdAt: { type: Number },
      updatedAt: { type: Number },
    },
    {
      timestamps: { currentTime: () => new Date().valueOf() },
    }
  );

  const M = mongoose.model<Test>("Test", schema);

  await M.create({ testProp: "hello" });

  const myDoc = await M.findOne({}).select({ _id: 0, renamed: "$testProp" }).lean().exec();

  /*
  myDoc successfully returns { renamed: "hello" }

  But we get the following Typescript error anyway:

  Argument of type '{ _id: number; renamed: string; }' is not assignable to parameter of type 'string | string[] | Record<string, number | boolean | object>'.
  Types of property 'renamed' are incompatible.
    Type 'string' is not assignable to type 'number | boolean | object | undefined'.
  */
};

@vkarpov15 vkarpov15 merged commit 31e0a69 into Automattic:master Feb 22, 2024
3 checks passed
@vkarpov15 vkarpov15 added this to the 8.1.4 milestone Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants