How to define a partial object with JSDoc (make all properties optional)

Viewed 864

How do I define a type that accepts some or all of an object's properties? I know I can mark each property as optional, but is there a way to make all properties optional?

const defaults = {
    host: 'localhost',
    distDir: 'dist',
    entrypoint: '__app.html',
    app: `build/bundle.js`,
    spaPort: "5000",
    ssrPort: "5005",
    serveSpa: false,
    serveSsr: false,
}

/** 
* @param {defaults} options 
*/
function merge(options = {}){
  const allOptions = {...defaults, ...options)
}

The above code shows a warning as {} doesn't satisfy defaults. Can I define the param _options to accept any subset of defaults?

1 Answers

Solved with

/** @param {Partial<defaults>} options */
Related