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

How to pass an array of objects in Typescript NextJS with interfaces IntrinsicAttributes error

I am using NextJS with typescript. In the main async page, I get the client Quotes
const clientQuotes: IQuote[] = await getAllQuotes();.

The IQuote interface have the following form:

export interface IQuote {
    id:                 string          
    dateTimeAdded:      string
    hasBeenPaid:        boolean
    completed:          boolean
    dateTimeNeeded:     string  
    nameClient:         string     
    numberOfVechicles:  number
    serviceRequired:    string  
    phoneNumber:        string   
    clientEmail:        string   
    detailsOfQuote:     string    
};

The response I get is a normal array of objects. [{}, {}], filled with the info above.

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

Next, I created another component where I passed the array with a prop quotes. However, this prop is highlighted in red. <UncompletedQuotes quotes={clientQuotes}/>

The error is Type '{ quotes: IQuote[]; }' is not assignable to type 'IntrinsicAttributes & IQuoteList'. Property 'quotes' does not exist on type 'IntrinsicAttributes & IQuoteList'.ts(2322), which I do not understand…

import React, { FC } from 'react'

export interface IQuote {
    id:                 string          
    dateTimeAdded:      string
    hasBeenPaid:        boolean
    completed:          boolean
    dateTimeNeeded:     string  
    nameClient:         string     
    numberOfVechicles:  number
    serviceRequired:    string  
    phoneNumber:        string   
    clientEmail:        string   
    detailsOfQuote:     string    
};

interface IQuoteList extends Array<IQuote>{};



const UncompletedQuotes: FC<IQuoteList> = (quotes) => {
  
  console.log(quotes);
  

  return (
    <section>
    </section>
  )
}

export default UncompletedQuotes

The problem is also that if I log in console the prop sent I get an object that contains a key called quotes. So instead to have a simple array of objects I get something of the following form:
{quotes: [{id, dateTime, etc}, {id, dateTime, etc}]}

>Solution :

Try this:

export interface IQuote {
    id:                 string          
    dateTimeAdded:      string
    hasBeenPaid:        boolean
    completed:          boolean
    dateTimeNeeded:     string  
    nameClient:         string     
    numberOfVechicles:  number
    serviceRequired:    string  
    phoneNumber:        string   
    clientEmail:        string   
    detailsOfQuote:     string    
}

// here you assign all props your function/component will have
// this will be the type of your function object param
type ComponentType = {
  quotes: IQuote[]
  // you can add as many props you want
  otherProp: number
  anotherProp: string
}

// your function/component will receive an object as ComponentType 
// and we are destructuring it in function declaration...
function UncompletedQuotes({
  quotes,
  otherProp,
  anotherProp
}: ComponentType): JSX.Element {
  
  console.log(quotes)
  
  return (
    <section>
    </section>
  )
}


// ... or you can create the function and destruct params object later
function UncompletedQuotes(params: ComponentType) {
  const {
    quotes,
    otherProp,
    .... etc
  } = params

  ....
}

export default UncompletedQuotes

Hope this helps you 😀

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