question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set default value with typescript

See original GitHub issue

Do 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:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Jan 17, 2022

I took a closer look and any[] is correct for Types.Array.push(). Because of Mongoose type casting and change tracking, the below are perfectly valid:

interface User {
  oids: Types.Array<Types.ObjectId>[];
}

const user = new UserDoc({});

user.oids.push('000000000000000000000000'); // Perfectly fine to push a string, Mongoose casts it to ObjectId if possible
user.oids.push(user); // Perfectly fine to push a Document, Mongoose casts it to ObjectId

user.oids.push('fail'); // Push something invalid
user.oids[user.oids.length - 1] = new Types.ObjectId('000000000000000000000000'); // Overwrite the previous invalid oid, and now you can save

await user.save(); // Succeeds
0reactions
imranbarbhuiyacommented, Jan 8, 2022

yeah that makes sense. For pure array image

Btw, Types.Array makes it any type, so ig that needs a fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Default Parameters
Use default parameter syntax parameter:=defaultValue if you want to set the default initialized value for the parameter. · Default parameters are optional. ·...
Read more >
Setting default value for TypeScript object passed as argument
The trick is to first put in brackets what keys you want to pick from the argument object, with key=value for any defaults....
Read more >
Using Default values with Interfaces in TypeScript | bobbyhadz
To set default values for an interface in TypeScript, create an initializer function, which defines the default values for the type and use...
Read more >
How To Set A Default Parameter In TypeScript? - Tim Mousk
You can pass a function result as the default value to a default parameter. Here is an example of how to do it:...
Read more >
TypeScript How to set a default value with Interface
This page shows 3 ways to set default values to an interface. Using Spread Operator is an easy way. You can also define...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found