Object for Optional Function Arguments in Typescript

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 }

--

--

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