Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
    )
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading