设置类型的对象阵列
我正在努力弄清楚我的代码问题是什么。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
history []
或array&lt; history&gt;
而不是[历史>]
,这意味着您需要一系列history < /code>类型或每个元素类型为
历史记录
的数组。[历史]
表示它具有一个数组,该数组将包含类型历史> history> history>
history [] 的元素,表示您是类型<的数组代码>历史记录
,也可以做:
You can use either
history[]
orArray<history>
instead of[history]
which means you want an array ofhistory
type or an array in which each elements will be of typehistory
.[history]
means that it has an array that will contain an element of typehistory
history[]
means you it is an array of typehistory
or you can also do as: