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

203
Views
how to make optional object parameter in Typescirpt?

i have a function with following shape

const func = (arg1: string, { objArg = true }:{ objArg: string }) => { // some code }

i need to make second parameter (object) optional, is it possible ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can assign an empty object {} as default param value to it. Wrapping object param type with Partial to make its memebers optional, same as adding question mark { objArg?: string }

const func = (arg1: string, { objArg = true }: Partial<{ objArg: string | boolean }> = {}) => {
  console.log(objArg)
}
about 4 years ago · Juan Pablo Isaza Report

0

You can set an default value and it will be automatically optional:

const func = (
  arg1: string,
  { objArg = true }: { objArg: boolean } = undefined
) => {}
about 4 years ago · Juan Pablo Isaza Report

0

If you want to make an parameter optional, you should use ?(question mark) before :
and optional parameter has to be the last parameter of function. For more information check this page. https://www.typescripttutorial.net/typescript-tutorial/typescript-optional-parameters/

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!