Combining Default and Custom Options in Typescript

Ole Ersoy
Nov 1, 2021

--

Image by Pexels from Pixabay

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

--

--

Ole Ersoy

Founder of Firefly Semantics Corporation