Skip to content

[REG 6->7] Plain fields cannot be assigned in special case (typescript error) #13529

@orgads

Description

@orgads

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.3.0

Node.js version

18.16.0

MongoDB server version

5.x

Typescript version (if applicable)

5.1.3

Description

Directly assigning to fields of a document fails with v7 when the document is generic (special case, see example).

Steps to Reproduce

import { Model } from 'mongoose';

interface ResourceDoc {
  foo: string;
}

type ResourceIdT<ResourceIdField extends string> = {
  [key in ResourceIdField]: string;
}
type ResourceDocWithId<ResourceIdField extends string> = ResourceDoc & ResourceIdT<ResourceIdField>;

class TestClass<
  ResourceType extends string,
  DocT extends ResourceDocWithId<`${ResourceType}Id`>>
{
  constructor(dbModel: Model<DocT>) {
    const resourceDoc = new dbModel();
    resourceDoc.foo = 'bar'; // Type 'string' is not assignable to type 'Require_id<DocT>["foo"]'
  }
}

If I replace `${ResourceType}Id` with 'FooId' then it works.

Expected Behavior

It should work.

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