Object for Optional Function Arguments in Typescript

Ole Ersoy
1 min readApr 23, 2020

--

Image by Gerd Altmann from Pixabay

Scenario

We have a paging API that returns Customer instances.

We want API users to be able to specify paging options without worrying about the order of the Javascript arguments passed in.

Design

export function fetchCustomers(id:string, options?:PagingOptions):Customer[]

And the PagingOptions interface looks like this:

interface PagingOptions {
limit?:number
after?:string
}

We can now just check the PagingOptions argument for the optional parameters:

if (options.limit) { //limit the return result }

--

--

Ole Ersoy

Founder of Firefly Semantics Corporation