设置类型的对象阵列

发布于 2025-02-12 22:07:58 字数 1047 浏览 1 评论 0原文

我正在努力弄清楚我的代码问题是什么。

interface history { 
  data: string, 
  przed: string | number, 
  po: string | number,
}

我在功能组件之外有一个接口,然后在内部我定义了应该的外观 - 没问题

const [history, setHistory] = useState<[history]>([{
    data: '',
    przed: '',
    po: '',
}]);

,然后我有一个单击功能,可以

    setHistory([ ... history , {
      data: new Date().toISOString().slice(0, 10), // date
      przed: fromTo, // number
      po: valueToSave, // number
    }]);

在浏览器中编译和测试时,一切正常,但是VS代码不喜欢我与此错误的代码:

Argument of type '[history, { data: string; przed: number; po: string; }]' is not assignable to parameter of type 'SetStateAction<[history]>'.
  Type '[history, { data: string; przed: number; po: string; }]' is not assignable to type '(prevState: [history]) => [history]'.
    Type '[history, { data: string; przed: number; po: string; }]' provides no match for the signature '(prevState: [history]): [history]'.ts(2345)

如何处理这样的事情?这是什么意思?

I'm struggling to figure out what the problem is with my code.

interface history { 
  data: string, 
  przed: string | number, 
  po: string | number,
}

I have an interface outside of the Functional Component then inside I define how it's supposed to look like - no problem

const [history, setHistory] = useState<[history]>([{
    data: '',
    przed: '',
    po: '',
}]);

then I have a onClick function which does this:

    setHistory([ ... history , {
      data: new Date().toISOString().slice(0, 10), // date
      przed: fromTo, // number
      po: valueToSave, // number
    }]);

Everything works fine when compiled and tested in the browser, however VS Code is not liking my code at all with this error:

Argument of type '[history, { data: string; przed: number; po: string; }]' is not assignable to parameter of type 'SetStateAction<[history]>'.
  Type '[history, { data: string; przed: number; po: string; }]' is not assignable to type '(prevState: [history]) => [history]'.
    Type '[history, { data: string; przed: number; po: string; }]' provides no match for the signature '(prevState: [history]): [history]'.ts(2345)

How can I handle something like this? What does it mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2025-02-19 22:07:58

您可以使用history []array&lt; history&gt;而不是[历史>],这意味着您需要一系列history < /code>类型或每个元素类型为历史记录的数组。

[历史]表示它具有一个数组,该数组将包含类型历史> history> history> history [] 的元素

,表示您是类型<的数组代码>历史记录

const [history, setHistory] = useState<history[]>([
    {
      data: "",
      przed: "",
      po: ""
    }
  ]);

,也可以做:

  const [history, setHistory] = useState<Array<history>>([
    {
      data: "",
      przed: "",
      po: ""
    }
  ]);

You can use either history[] or Array<history> instead of [history] which means you want an array of history type or an array in which each elements will be of type history.

[history] means that it has an array that will contain an element of type history

history[] means you it is an array of type history

const [history, setHistory] = useState<history[]>([
    {
      data: "",
      przed: "",
      po: ""
    }
  ]);

or you can also do as:

  const [history, setHistory] = useState<Array<history>>([
    {
      data: "",
      przed: "",
      po: ""
    }
  ]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文