Skip to content

Given the type 'T & Document', can't type the object returned by .toObject() as T #9944

@adriano-di-giovanni

Description

@adriano-di-giovanni

I don't know if it is a bug. It seems to be so. I can tell you it worked with v5.10.11. It doesn't work anymore with v5.11.16

Given this piece code

function transformOne<T>(document: T & Document): T {
  return document.toObject();
}

the compiler complaints as follows

Type 'Pick<Pick<_LeanDocument<T & Document<any>>, "_id" | "__v" | "id" | Exclude<keyof T, "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | ... 46 more ... | "$isSingleNested">>, Exclude<...> | ... 2 more ... | Exclude<...>>' is not assignable to type 'T'.
  'T' could be instantiated with an arbitrary type which could be unrelated to 'Pick<Pick<_LeanDocument<T & Document<any>>, "_id" | "__v" | "id" | Exclude<keyof T, "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | ... 46 more ... | "$isSingleNested">>, Exclude<...> | ... 2 more ... | Exclude<...>>'.ts(2322)

My tsconfig.json is as follows

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions