tsed: @Default decorator not working with falsy values

@Model(...)
export class Product {

    @Default(false or 0) // <-- setting this option to false or 0 takes no effect... 
    availability: ?

 ....

    @Default(true | -1 | 'string') // <-- setting this option to true -1 or any string: works as expected.. 
    availability: ?

}

the model created results in something like this

/* 1 */ using @Default(true)
{
    "_id" : ObjectId("5ae04db8dd3c80e7efdb5e98"),
    "availability" : true, 
    "version" : 0
}
/* 2 */ using @Default(false)
{
    "_id" : ObjectId("5ae04dd0ba74c9e7f56e9548"),
    "version" : 0
}

i believe that somewhere it is actually doing a if(value) check… to see if that option is empty or no but it is failing because the value is actually false…

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@milewski Default is used only to set metadata. It didn’t change the behavior of your property.

According to the documentation, the default value must be also assigned to your property:

class Model {
  @Default("10")
   property: string = "10";
}

I agree, it’s redundant XD

See you 😉