react.js JSON 数组的打字稿问题

react.js typescript problem with JSON Array

我正在努力让自己熟悉 react.js 打字稿。 我试图声明一个 JSON 数组,但它给了我错误消息 ... is not assignable to JSON

这是我的代码:

import React from 'react';
type MyProps = {
    message?: string;
};
type MyState = {
    chat_list : Array<JSON>
    count: number; // like this
};
class ChatList extends React.Component<MyProps, MyState> {
    state: MyState = {
        count: 0,
        chat_list : [
            {
                "name":"true",
                "active" : true
            }
        ]
    };
    ...

我该如何解决?

您应该定义聊天项的形状,JSON 是具有特定形状的实际全局对象(JSON.stringifyJSON.parse 等)

    interface ChatItem {
      name: string;
      active: boolean;
    }

    interface MyState {
      chat_list: Array<ChatItem>; // Or ChatItem[]
      count: number;
    }

   state: MyState = {
      count: 0,
      chat_list: [
        {
          name: 'true',
          active: true,
        },
      ],
    };