Bussiere
Bussiere

Reputation: 1164

Define a parameter as an array of specifics types in typescript3

here is my code :

import * as React from 'react';

export const Suggestions = (props:any) => {
  const options = props.results.map((r: { id: number ; name: string; }) => (
    <li key={r.id}>
      {r.name}
    </li>
  ))
  return <ul>{options}</ul>
}

export default Suggestions

but i would like to define r as an array of { id: number ; name: string; }

not just { id: number ; name: string; }

Regards

Upvotes: 1

Views: 34

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115282

You can use [] at the end of the inline interface definition to make it as an array type.

const options = props.results.map((r: { id: number ; name: string; }[]) => ( 


Or you can use Array<T> with the inline interface defenition.

const options = props.results.map((r: Array<{ id: number ; name: string; }>) => ( 

Upvotes: 2

Related Questions