yooneskh
yooneskh

Reputation: 813

TypeScript Generic type intersection does not satisfy itself

I have a problem with generic types. its simplified version is as this.

import { Document, Model } from 'mongoose';

interface Base {
  age: number;
}

class Test<T extends Base> {
  private t: Model<T & Document>
}

i get an error for this part Model<T & Document> which says this:

Type 'T & Document<any, {}>' does not satisfy the constraint 'Document<any, {}>'

Does anyone has any idea?

Upvotes: 0

Views: 203

Answers (1)

zixiCat
zixiCat

Reputation: 1059

That because the type of Document is class Document<...>, that is why you can't use intersecion type & with the "Object" type directly.

To solve this issue, you could use extends to extend the class like following:

class Base extends Document {
  age?: number;
}

class Test<T extends Base> {
  private t?: Model<T>;
}

Upvotes: 1

Related Questions