简体   繁体   English

TS2739 类型“any[]”缺少来自“JSON”类型的以下属性:解析、字符串化、[Symbol.toStringTag]

[英]TS2739 Type 'any[]' is missing the following properties from type 'JSON': parse, stringify, [Symbol.toStringTag]

In my react-typescript project I am running into a typing error when I try to pass a JSON as a prop.在我的 react-typescript 项目中,当我尝试将JSON作为道具传递时遇到了打字错误。

class MainNewsFeed extends React.Component {
    constructor(props: any) {
        super(props);
    }

    render() {
        const bitcoinJson = [...require("./feeds/newsJson.json")];
        return (
            <NewsFeedCol json={newsJson}/>
        );
    }
}

class NewsFeedCol extends React.Component<{ newsJson:JSON },  {}> {
    constructor(props: any) {
        super(props);
        let JsonData = [...props.json]
    }
    render() {
        return (
            <Row>
                <Col sm={12} md={6} lg={4}>
                    <NewsFeedItem index={1}/>
                </Col>
            </Row>
        );
    }
}


/Users/simon/Code/web/thedrewreport/frontend/thedrewreport/src/MainNewsFeed.tsx
TypeScript error in /Users/simon/Code/web/thedrewreport/frontend/thedrewreport/src/MainNewsFeed.tsx(18,26):
Type 'any[]' is missing the following properties from type 'JSON': parse, stringify, [Symbol.toStringTag]  TS2739

    17 |         return (
  > 18 |             <NewsFeedCol json={bitcoinJson}/>
       |                          ^
    19 |         );
    20 |     }
    21 | }

How do you properly handle typing here?你如何正确处理在这里打字?

It seems like you want to annotate the newsJson to be of type object.似乎您想将newsJson注释为newsJson类型。 However, JSON a module (which has JSON.parse and JSON.stringify), not a type .但是, JSON是一个模块(具有 JSON.parse 和 JSON.stringify),而不是 type To annotate newsJson attribute, you will need to know the exact shape of the object that you want, probably make it into an interface .要注释newsJson属性,您需要知道您想要的对象的确切形状,可能将其制作成interface For example例如

interface NewsFeedsColProp {
  newsJson: {
     name: string;
     provider: string;
     id: number;
   }
}

class NewsFeedCol extends React.Component<NewsFeedsColProp ,  {}>

Note the fields name , provider , and id - I am expecting the newsJson to be an object with these 3 fields.请注意字段nameproviderid - 我希望newsJson是具有这 3 个字段的对象。

The name , provider , and id fields are just made-up things of mine; nameproviderid字段只是我自己编造的; you need to determine the exact shape of the data that your newsJson prop is going to get.您需要确定newsJson道具将获得的数据的确切形状。

If you can't know the exact shape, you can type newsJson as any type, or "unknown-object" type: {[key: string]: any} - which is not really a good practice如果您不知道确切的形状,您可以将newsJsonany类型,或“未知对象”类型: {[key: string]: any} - 这不是一个很好的做法

interface NewsFeedsColProp {
  newsJson: {
    [key: string]: any,
  }
}

// Or
interface NewsFeedsColProp {
  newsJson: any
}

class NewsFeedCol extends React.Component<NewsFeedsColProp ,  {}>

Hope I am making sense希望我说的有道理

暂无
暂无

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

相关问题 错误 TS2739:类型“{}”缺少类型中的以下属性 - error TS2739: Type '{}' is missing the following properties from type ESlint 错误,输入 &#39;() =&gt; Promise<void> &#39; 缺少类型 &#39;Promise 的以下属性<void> &#39;:然后,抓住,[Symbol.toStringTag],最后 - ESlint error, Type '() => Promise<void>' is missing the following properties from type 'Promise<void>': then, catch, [Symbol.toStringTag], finally 类型“{}”缺少类型 ts(2739) 中的以下属性 - Type '{}' is missing the following properties from type ts(2739) Typescript:TS2739:缺少属性 - Typescript: TS2739: properties are missing 类型 &#39;Object&#39; 缺少类型 &#39;any[]&#39; 的以下属性:length、pop、push、concat 和 26 more.ts - Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts TS 错误:“事件”类型缺少“键盘事件”类型中的以下属性 - TS error: Type 'event' is missing the following properties from type 'keyboardevent' TS2740 缺少 type、Typescript、NestJS 中的以下属性 - TS2740 is missing the following properties from type, Typescript, NestJS 如何访问Symbol(Symbol.toStringTag)属性? - How to access the Symbol(Symbol.toStringTag) property? 类型“订阅”缺少类型“可观察”的以下属性 <StringMap<any> &gt;” - Type 'Subscription' is missing the following properties from type 'Observable<StringMap<any>>' Angular - 类型“{}”缺少类型“any[]”的以下属性 - Angular - Type '{}' is missing the following properties from type 'any[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM