0

I have an interface Contact which extends mongoose.Document interface:

import * as mongoose from 'mongoose';

export interface Contact extends mongoose.Document {
    firstname: string;
    lastname: string;
    address: string;
    phone: string;
    email: string;
}

And sometimes, I need to use this interface Contact like below:

const contacts: Contact[] = [
    {
        firstname: 'Marley',
        lastname: 'Schultz',
        address: '531 Walter Roads',
        phone: '1-898-444-3407 x33699'
        email: 'Deon_Hammes@gmail.com',
    }
];

Since Contact extends mongoose.Document, TypeScript complains that mongoose.Document ' s properties ( increment, model, isDeleted, ... ) are missing.

Is there a way to get a type from Contact but with mongoose.Document ' s properties excluded ?

Faly
  • 13,291
  • 2
  • 19
  • 37
  • 3
    An interface can extend multiple other interfaces, why not have `interface Contact { ... }` with the raw properties and `interface ContactDocument extends Contact, mongoose.Document {}`? – jonrsharpe Feb 23 '19 at 09:45
  • @jonrsharpe A good workaround, thank you! – Faly Feb 23 '19 at 09:59
  • 1
    You can use Pick to pick out particularly properties of an interface, or build an Omit type which omits certain types: https://stackoverflow.com/questions/48215950/exclude-property-from-type – Richard Feb 23 '19 at 11:45

0 Answers0