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 LeanDocument seems not working for Array of Array #10293

Closed
lutherman opened this issue May 26, 2021 · 2 comments
Closed

Type LeanDocument seems not working for Array of Array #10293

lutherman opened this issue May 26, 2021 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@lutherman
Copy link

Hi,

The type LeanDocument seems not working for Array of Array.

I have some source codes like that:

const SheetSchema = new Schema<Sheet, SheetModel> ({
  name: {
    type: String,
    required: true,
  },
  projectId: { type: Schema.Types.ObjectId },
  folderId: { type: Schema.Types.ObjectId },
  search: [new SheetSavedSearchSchema()],
});

const SheetSavedSearchSchema = new Schema ({
  searchName: String,
  searchCriteria: [[new SearchCriteriaSchema()]], // <-- Array of Array !!!
});

And I get its LeanDocument by function toObject:

const sheet: Sheet = sheetDoc.toObject();

However, the type error occurs:

Type 'LeanDocument<Sheet & Document<any, any>>' is not assignable to type 'Sheet'.
  Types of property 'search' are incompatible.
    Type 'LeanDocument<SheetSavedSearch>[]' is not assignable to type 'SheetSavedSearch[]'.
      Type 'LeanDocument<SheetSavedSearch>' is not assignable to type 'SheetSavedSearch'.
        Types of property 'searchCriteria' are incompatible.
          Type 'LeanDocument<SearchCriteria[]>[]' is not assignable to type 'SearchCriteria[][]'.
            Type 'LeanDocument<SearchCriteria[]>' is missing the following properties from type 'SearchCriteria[]': [Symbol.iterator], [Symbol.unscopables]ts(2322)

Did i do something wrong on schema declaration?


nodejs: 12.19.0
mongodb: 4.2.7
mongoose: 5.12.11

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label May 26, 2021
@IslandRhythms
Copy link
Collaborator

before I attempt to repro this, try swapping the position of your two schemas. SheetSavedSearchSchema is used before it is declared so that could be causing your problem. Also, if the problem still persists please include other things not defined like the search criteria schema

@IslandRhythms IslandRhythms added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label May 26, 2021
@lutherman
Copy link
Author

I wrote a test code to simplify the situation.

Test code:

import { Model, Schema } from 'mongoose';

interface ITest {
  name: string;
  arrayOfArray: string[][]; // <-- Array of Array
}

const testSchema = new Schema<ITest, Model<ITest>>({
  name: {
    type: String,
    required: true,
  },
  arrayOfArray: [[String]],
});

testSchema.methods.getArrayOfArray = function (): string[][] { // <-- function to return Array of Array
  const test = this.toObject();

  return test.arrayOfArray; // <-- error here
};

And its error message:

Type 'LeanDocument<string[]>[]' is not assignable to type 'string[][]'.
  Type 'LeanDocument<string[]>' is missing the following properties from type 'string[]': [Symbol.iterator], [Symbol.unscopables]ts(2322)

If the field is single array, will be OK.

@vkarpov15 vkarpov15 removed the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label May 31, 2021
@vkarpov15 vkarpov15 added this to the 5.12.13 milestone May 31, 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