xkeshav
xkeshav

Reputation: 54050

typings for object parameters function structure

How to set typing for object style parameters?

I have below function signature

private buildURI({ endpoint params }): void {

}

now typescript throw error for missing typings, so I have tried this

private buildURI({ endpoint:string, params: any[] }): void { }

also

private buildURI({ endpoint, params }: { string, any[]}): void { }

but both did not work, only this one works

private buildURI({ endpoint, params }: any ): void { }

but it does not seem to be a valid one.

so how to set this method with valid typing?

Upvotes: 0

Views: 46

Answers (2)

Lazar Ljubenović
Lazar Ljubenović

Reputation: 19764

The correct method to set "object-styled" (the correct term is destructed parameters) is the following.

private buildURI({ endpoint, params }: { endpoint: string, params: any[] })

It's a known pain-point discussed here.

Of course, you can always define an interface first and then use it.

interface UriOptions {
  endpoint: string
  params: any[]
}

private buildURI({ endpoint, params }: UriOptions)

Upvotes: 3

Aleksey L.
Aleksey L.

Reputation: 37986

You should define a type having endpoint and params props:

type BuildUriRequest = {
    endpoint: string,
    params: any[]
}

function buildURI({ endpoint, params }: BuildUriRequest): void { }

Upvotes: 1

Related Questions