Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!