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

Type Definition of FlattenMap isn't working for non trivial types such as Date and ObjectId #11117

Closed
borfig opened this issue Dec 17, 2021 · 0 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@borfig
Copy link
Contributor

borfig commented Dec 17, 2021

Do you want to request a feature or report a bug?

bug

What is the current behavior?

The type definition of FlattenMaps makes it impossible to be used with ObjectId and Date fields (and maybe other non-trivial types?)

If the current behavior is a bug, please provide the steps to reproduce.

The following TypeScript fails to build:

import { Model, Schema, Types, model  } from "mongoose";
interface Foo {
  someDate: Date;
  someId: Types.ObjectId;
  someNumber: number;
  someString: string;
}
const fooSchema = new Schema<Foo, Model<Foo>>({
  someDate: { required: true, type: Date },
  someId: { required: true, type: Schema.Types.ObjectId },
  someNumber: { required: true, type: Number },
  someString: { required: true, type: String },
});
const fooModel = model("foos", fooSchema);
async function someLogic() {
  const items = await fooModel.create<Foo>([
    {
      someId: new Types.ObjectId(),
      someDate: new Date(),
      someNumber: 5,
      someString: "test",
    },
  ]);
  const json = items[0].toJSON();
  const someDate: Date = json.someDate; // Type 'FlattenMaps<Date>' is not assignable to type 'Date'
  const someId: Types.ObjectId = json.someId; // Type 'FlattenMaps<ObjectId>' is not assignable to type 'ObjectId'
  const someNumber: number = json.someNumber; // OK
  const someString: string = json.someString; // OK
  console.log(someDate);
  console.log(someId);
  console.log(someNumber);
  console.log(someString);
}

My tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2020",
    "lib": [
      "dom",
      "es2020",
    ],
    "esModuleInterop": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "jsx": "react"
  },
  "include": [
    "src/**/*.ts*",
    "src/locales/**/*.json"
  ]
}

What is the expected behavior?

FlattenMaps<T> needs to be exactly T where T is Date or Types.ObjectId

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Note.js: 4.18.2 (although I don't believe it is relevant to this bug)
Mongoose: 6.1.2 (I believe the issue exist since 6.0.12)
MongoDB: 4.4.10 (although I don't believe it is relevant to this bug)

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Dec 17, 2021
@vkarpov15 vkarpov15 added this to the 6.1.6 milestone Dec 23, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.1.6, 6.1.5 Dec 30, 2021
vkarpov15 added a commit that referenced this issue Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants