Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
Setting a default property value in a type literal in TS

I have the following TS class:

export class TestClass {
   constructor(public options: { a?: number, b?: string, c?: boolean, d?: number }) { }
}

Implementations:

 new TestClass({a:123});
 new TestClass({b:'hello'});

I am looking for a way to set default values for each of the options properties (a, b, c etc.), so that developer can send only part of the parameters, and the other unassigend parameters will get their defaults.

This option is not available:

 constructor(public options: { a?: number = 0, b?: string = '', c?: boolean = false, d?: number = 1 }) { }

because of the "A type literal property cannot have an initializer in TS" restriction.

So the way I implement it now is the following:

 constructor(public options: { a?: number, b?: string, c?: boolean, d?: number }) {
         !options.a && (options.a = 0),
         !options.b && (options.b = ''),
         !options.c && (options.c = false),
         !options.d && (options.d = 1)
}

My problem is that in the real world the options parameter contains many properties, and I don't want to force each property to be written twice, in the constructor signature and in the constructor's body.

My question is if there is a better way to do it.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Default parameters are used for function/method arguments that are undefined (both for JavaScript and TypeScript), however in your case you have a parameter value with optional properties inside of it. Setting those properties to default values is unfortunately natively not possible.

However, we can slightly improve the syntax of your constructor:

constructor(public options: { a?: number, b?: string, c?: boolean, d?: number }) {
  options.a || options.a = 0;
  options.b || options.b = '';
  options.c || options.c = false;
  options.d || options.d = 1;
}

If you are using ES2020+, you can use logical nullish assignment to make your syntax even cleaner:

constructor(public options: { a?: number, b?: string, c?: boolean, d?: number }) {
  options.a ??= 0;
  options.b ??= '';
  options.c ??= false;
  options.d ??= 1;
}

More info can be found here.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda