Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

class-transformer default value

import { Transform, Expose } from 'class-transformer'
import { cloneDeep } from 'lodash'

export function Default(defaultValue: unknown): PropertyDecorator {
  return Transform((value: unknown) => value ?? cloneDeep(defaultValue))
}
                   
class A {
  @Expose()
  @Default('123')
  a: string
}
 
PREVIOUS NEXT
Tagged: #default
ADD COMMENT
Topic
Name
3+4 =