No. You will notice that as soon as we installed this package the error goes away because typescript now has access to the type definition file, and now it can use this file to check our code for errors and the types required when we work with the express package. How does the DB know what models there are? In this tutorial we have learnt the following: I’m open to suggestions and corrections on this tutorial, For the more experienced ones, If there is anything you think I didn’t explain quite well or any contribution you will like to make to this tutorial to make it better, kindly drop your comments in the comment section, I’ll be glad to learn from you too, and the rest of us reading this article would also have an opportunity to learn from your experience. if you are having issues with this or this is throwing an error, install typescript globally like this: This will create the tsconfig file for us, tsconfig specifies the root folder and the compiler option to compile our typescript code down to javascript. mkdir src/models. Let’s do that, in the same file routes/todo.ts, type this code: Like the explanation we gave above, we are extending the existing Document interface, the extend keyword means, “take all properties of the existing mongoose.Document interface, add them to our new interface TodoDoc”, then we include some other properties such as title and description as part of the properties in the TodoDoc, now the TodoDoc contains all the properties of our mongoos.Document interface and title, which is a string, and description which is also a string, if mongoose adds extra properties such as createdAt and updatedAt, then we’ll define them in the TodoDoc interface like we defined title and description. To get started, create a Node project and add TypeScript and Typegoose as dependencies. If the model changes, you also have to keep the TypeScript interface file in sync or the TypeScript interface would not represent the real data structure of the Mongoose model. We’ll be looking at these issues and creating a fix for them, but before continue, let’s connect our application to the database first. Welcome back to the land of coding, I hope it’s treating you alright . Type ‘IContact’ is missing the following properties from type ‘Document’: increment, model, isDeleted, remove, and 51 more.ts(2344). we want the req and res property to only be of type express.Request and type express.Response respectively. if we hover over it we’ll see that typescript is telling us that it cannot find a file declaration for mongoose module. we can always edit this file to modify the default configuration, but we’ll go with the default configuration in this tutorial. The final source code for this tutorial can be found HERE. Now this works fine, and this solves two of our problems that relate to properties and data types. I have the code to this article on github, you can fork it here, and you can follow me too. I’m just getting back into coding (previous experience with Java & C#) after a number of years. Now let’s import mongoose in our index.ts file, add the following code. Also, Notice the angle bracket , I am not going to go deep into the details of this, this is called generics in typescript, you can read more about it, but imagine it like a function definition that takes in a parameter and a function call that takes in an argument, in this case, we are calling the mongoose.Model interface and it needs additional information(the argument we passed in) that will be used in the interface body(mongoos.Model interface), the argument we are passing, in this case, is a type(generics take in types as arguments), and it is a type of “any”, we’ll be changing this later, but let’s see the reason why we are passing in the “any” type for now. We have only defined this interface, we are not using it yet. how to add types to function, variables, parameters. Welcome back to the land of coding, I hope it's t... Nicholas, great article – thank you! A working example of Typegoose in action. My guess would be that you are extending IContact, which you are also simultaneously casting (from a mongoose Document) to a mongoose Model. Also, Notice that we are not using commonjs require function, instead, we are using the import statement to import our code, typescript prefers to use the import statement over the require statement, otherwise, we’ll see an error thrown, you can try this on your own. so if I have a function that takes a parameter called name which should be a type of string, I’ll do something like this: As you can see, name is a parameter, and it should be a string according to the function above, the same way, if I am to use my todo Interface in this function on the name parameter then the function will look like this: This means our name parameter is expecting an argument which must be an object that contains a property title, which must be a string, and description property which must also be a string, remember these are properties listed in our ITodo interface. After that create a Mongoose connection to your MongoDB instance. this means that an interface defines the shape of an object, the properties it should have, the type of the properties, and functions. let’s do that: As soon as we did this you can see that the error is completely gone. Also, another issue arises when we create a new instance of our mongoose model like this: As you can see from the result, mongoose returns additional properties(createdAt, updatedAt) which typescript may not be aware of, we need a wat to tell typescript that there may be some additional properties that get created behind the scenes by mongoose when we create a new instance of our model. Last on the list now is to import the database controller. I’ll be glad if you follow me on medium too . when we are writing typescript we are still writing javascript, except in this case we are writing javascript with a type system. One of the first decisions was to use Mongooseas an easier way to model objects stored in the database. IMPORTANT! Because both technologies are so popular, it is not a surprise people want to use them together. Now typescript is aware of the object that must be passed into our model instance and will throw an error if something else is provided. but in our case, these extra properties are not added, Now to apply this interface, we’ll go to the part of our code where are creating our model, remember the first generic argument is a type of any, from our inspection earlier on we realised it should be a document, but since we didn't have a document interface defined yet we decided to use the type any, now we can use the TodoDoc instead of type any, Also, in our TodoModelInterface, our build function should return a document, but we are returning a type of any, now we can update it to use the TodoDoc interface, In the same TodoModelInterface, you will realise that the mongoose.Model also has a generic argument of type any, from our inspection we realised that this should also be a mongoose Document, let’s update it now to use our TodoDoc.