如何在Typescript NextJS中传递具有接口的对象数组IntrinsicAttributes错误

ct2axkht  于 2023-06-05  发布在  TypeScript
关注(0)|答案(1)|浏览(99)

我使用NextJS和Typescript。在主异步页面中,我得到客户端报价const clientQuotes: IQuote[] = await getAllQuotes();
IQuote接口具有以下形式:

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

我得到的响应是一个普通的对象数组。[{}, {}],填写上面的信息。
接下来,我创建了另一个组件,并在其中传递了带有prop quotes的数组。但是,此 prop 以红色突出显示。<UncompletedQuotes quotes={clientQuotes}/>
错误是Type '{ quotes: IQuote[]; }' is not assignable to type 'IntrinsicAttributes & IQuoteList'. Property 'quotes' does not exist on type 'IntrinsicAttributes & IQuoteList'.ts(2322),我不明白...

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

问题还在于,如果我登录控制台,prop发送我得到一个对象,其中包含一个名为quotes的键。因此,为了得到一个简单的对象数组,我得到了以下形式的东西:{quotes: [{id, dateTime, etc}, {id, dateTime, etc}]}

goqiplq2

goqiplq21#

试试这个:

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

希望对你有帮助:D

相关问题