简体   繁体   English

打字稿错误属性 x 在类型“只读”上不存在<Props> &amp; Readonly&lt;{ children?: ReactNode; }&gt;&#39;

[英]Typescript error Property x does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>'

I am new to Typescript .我是Typescript Using Nextjs, I have a basic component I would like to typecheck, however I am getting the error.使用 Nextjs,我有一个基本组件要进行类型检查,但是出现错误。 How can i typecheck my array of objects?如何对我的对象数组进行类型检查?

ERROR in C:/Users/Matt/sites/shell/pages/index.tsx(22,4):
22:4 Property 'swc' does not exist on type 'Readonly<Props> & Readonly<{ children?: ReactNode; }>'.
    20 |        render() {
    21 |                const {
    22 |                        swc: { results }
       |                        ^
    23 |                } = this.props;

type Props = {
    results: Array<any>;
};

Component:成分:

interface Props {
    swc: Object;
    results: Array<any>;
}
class swc extends React.Component<Props, {}> {
    static async getInitialProps() {
        const res = await fetch("https://swapi.co/api/people/");
        const swc = await res.json();

        return {
            swc
        };
    }

    render() {
        const {
            swc: { results }
        } = this.props;

        return results.map(swc => (
            <Layout>
    ...
            </Layout>
        ));
    }
}

export default swc;

this.props: this.props:

 { swc:
   { count: 87,
     next: 'https://swapi.co/api/people/?page=2',
     previous: null,
     results:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ] },
  url:
   { query: [Getter],
     pathname: [Getter],
     asPath: [Getter],
     back: [Function: back],
     push: [Function: push],
     pushTo: [Function: pushTo],
     replace: [Function: replace],
     replaceTo: [Function: replaceTo] } }

You already have the props object defined inside the swc class, no need to destruct the class itself.您已经在swc类中定义了props对象,无需销毁类本身。

You're essentially trying to access swc.props.swc.results when you should be accessing swc.props.results instead当您应该访问swc.props.swc.results时,您实际上是在尝试访问swc.props.results


In general though, this seems like an incorrect component set up and I'm confused by your props definition as you seem to be passing results prop and then swc to props, which is the component itself (?)但总的来说,这似乎是一个不正确的组件设置,我对您的props定义感到困惑,因为您似乎将results prop 然后swc传递给 props,这是组件本身(?)

It's hard for me to answer this without further detail, but I'd imagine you'd want to do something like this:如果没有进一步的细节,我很难回答这个问题,但我想你会想要做这样的事情:

import React, { Component } from 'react'
import fetch from 'isomorphic-unfetch'

type Props = {
   results: any[], // < try to be more specific than any[]
}

// React Components should per convention start with Uppercase
class Swc extends Component<Props, {}> {
  static async getInitialProps(ctx) {
     const res = await fetch("https://swapi.co/api/people/")
     const json = await res.json()
     return { json.swc.results }
  }

  render() {
     const { results } = this.props
     // React components should always return a singular tag 
     // (eg. div, React.Fragment, but not array of <Layout>
     const renderResults = results && results.map(result => (
         <Layout>
            {result}
         </Layout>
     ))

      return (
        <>
          {renderResults}
        </>
      )

  }
}

export default Swc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' &#39;Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; 类型上不存在属性“导航” }&gt; - Property 'navigation' does not exists on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }> 属性 &#39;google&#39; 不存在于类型 &#39;Readonly&lt;{ IGoogleMapTrackerProps }&gt; &amp; Readonly&lt;{ chirldren?: ReactNode; }&gt;&#39; - Property 'google' does not exist on type 'Readonly<{ IGoogleMapTrackerProps }> & Readonly<{ chirldren?: ReactNode; }>' "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 类型 &#39;{ props: ReactNode; 上不存在属性 &#39;className&#39; }&#39; - Property 'className' does not exist on type '{ props: ReactNode; }' “只读”类型上不存在属性“路由器” <props & routecomponentprops<{}, staticcontext, poormansunknown> &gt;</props> - Property 'router' does not exist on type 'Readonly<Props & RouteComponentProps<{}, StaticContext, PoorMansUnknown>> 将引用转发到 react.FC 给出类型错误:属性引用不存在于类型 &#39;IntrinsicAttributes &amp; Props &amp; { children :? 反应节点} - forwarding ref to react.FC gives type error: Property ref does not exist on type 'IntrinsicAttributes & Props & { children :? ReactNode} 类型“ Readonly &lt;{}&gt;”上不存在属性“ xxx” - Property 'xxx' does not exist on type 'Readonly<{}>' 类型“只读&lt;{}&gt;”上不存在属性“产品” - Property 'products' does not exist on type 'Readonly<{}>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM