Combining Default and Custom Options in Typescript

Scenario

We have a RANGE_DEFAULT_OPTIONS object:

const RANGE_DEFAULT_OPTIONS = { min: 0, max: 100 }

And we want to use the defaults, unless the API overrides them:

export function range(value:number, options:any) {
...
}

Approach

options = { ...RANGE_DEFAULT_OPTIONS, ...options }

Demo

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation