How to set default value with typescript
See original GitHub issueDo you want to request a feature or report a bug? It’s a question or probably a feature request.
What is the current behavior? Currently, I’ve a schema where date is default to Date.now. So it’ll never be undefined. But when I’m defining its interface if I use,
date: number;
Then when I’m saving a doc it always shows I’m missing date and if I use
date?:number
then when working with this model it shows date is possibly undefined. Currently, I’m using
// in interface
date?: number;
// when working with model
myModel.date as number
as a workaround, but what’s the appropriate way to do this? If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior? There should be a way to work with default values.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. node v16.13.0 mongoose v6.1.4 mongodb v4.2.2 typescript v4.5.4
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
I took a closer look and
any[]
is correct forTypes.Array.push()
. Because of Mongoose type casting and change tracking, the below are perfectly valid:yeah that makes sense. For pure array
Btw, Types.Array makes it any type, so ig that needs a fix.