Nemesis
Nemesis

Reputation: 351

Specifying the types of parameters in a function that takes 2 parameters

I have a function that takes two parameters as below. If they are in an object, I can specify their types as follows

    const renderItem = ({ item, index }: { item: string, index: number }) => {

        return (
            null
            )
    }

If parameters are not in an object like below, how can I specify their type?

const renderItem = (item, index) => {

    return (
        null
    )
}

Upvotes: 1

Views: 148

Answers (1)

Tobias S.
Tobias S.

Reputation: 23835

You can use a tuple type. This makes typing multiple parameters with a single type possible.

const renderItem = (...[item, index]: [string, number]) => {}

Or give each parameter its own explicit type.

const renderItem = (item: string, index: number) => {}

Playground

Upvotes: 2

Related Questions