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

164
Vistas
How can I fill up missing object parameters in an elegant way?

I am having trouble using initial parameters (I am not sure if there is a typescript solution or it is just a JavaScript question, so I present you the Javascript equivalent to you):

const helloWorldWithOptions = (options = { a:1, b:2 } ) => {
  console.log("Hello World!")
};

When using the function:

helloWorldWithOptions( {a:4} );

Typescript says that I am missing the object key b when using the function. What I would expect is that js/ts filling up the missing values with the initial parameters. If there is already a state of the art, let me know.

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

0

You can specify default values whenever you declare a function:

function aFunction({a = 1, b = 2}) {console.log(a, b)}

aFunction({}) //prints "1 2"

in this case, 'aFunction' receives an object, and whenever you call the function with an empty object, it automatically fills the blank fields. Alternatively, you can call 'aFunction' with the parameters you want, for example:

aFunction({a: 2, b: 3}) //prints "2 3"
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the Partial type and ... spreading to combine your defaults with the caller's overrides.

TS playground

const defaults = { a: 1, b: 2 };

// Could use a named type instead of `typeof defaults` too.
const helloWorldWithOptions = (options: Partial<typeof defaults> = {}) => {
  const mergedOptions = { ...defaults, ...options };
  console.log(mergedOptions);
};
helloWorldWithOptions({ a: 3 });

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