admin管理员组文章数量:1415484
I'm relatively new to TS, and I'm running into a weird issue. I seem to have defined everything, but it doesn't seem to recognize my typing for the data.
Here is what I have:
const Component = () => {
const [data, setData] = React.useState([[], []])
React.useEffect(() => {
fetchData()
}, [])
const fetchData = React.useCallback(async () => {
const data = await fetchDataController()
setData(data)
^^^^ error
})
}
This throws the error,
Argument of type 'Element[][]' is not assignable to parameter of type 'SetStateAction<never[][]>'
Type 'Element[][]' is not assignable to type 'never[][]'
Type 'Element[]' is not assignable to type 'never[]'
Type 'Element is not assignable to type 'never'
My function fetchDataController
return an array of arrays of elements.
My type is as follows:
type TData = {
id: string,
desc: string
}
I've tried doing something like,
const [data, setData] = React.useState<Array<TData[], TData[]>>([[], []])
But that didn't seem to work. What am I missing here?
I'm relatively new to TS, and I'm running into a weird issue. I seem to have defined everything, but it doesn't seem to recognize my typing for the data.
Here is what I have:
const Component = () => {
const [data, setData] = React.useState([[], []])
React.useEffect(() => {
fetchData()
}, [])
const fetchData = React.useCallback(async () => {
const data = await fetchDataController()
setData(data)
^^^^ error
})
}
This throws the error,
Argument of type 'Element[][]' is not assignable to parameter of type 'SetStateAction<never[][]>'
Type 'Element[][]' is not assignable to type 'never[][]'
Type 'Element[]' is not assignable to type 'never[]'
Type 'Element is not assignable to type 'never'
My function fetchDataController
return an array of arrays of elements.
My type is as follows:
type TData = {
id: string,
desc: string
}
I've tried doing something like,
const [data, setData] = React.useState<Array<TData[], TData[]>>([[], []])
But that didn't seem to work. What am I missing here?
Share Improve this question asked Aug 4, 2020 at 10:24 Mike KMike K 6,55117 gold badges76 silver badges145 bronze badges1 Answer
Reset to default 3You were close. Array<...>
requires one argument, the type of items in the array. So you could do something like Array<TData[]>
. If you don't want to use Array<...>
, you could do something like TData[][]
as well
So,
React.useState<TData[][]>([[], []])
Here's a working codesandbox with some mocks
If you want to be more specific, and define it as a Tuple, instead of usual array:
React.useState<[TData[], TData[]]>([[], []])
but for that to work, you'll have to assure Typescript that what you are setting i.e. data
in setData(data)
is also of type [TData[], TData[]]
.
You can do that with something like setData(data as [TData[], TData[]])
本文标签: javascriptTypescript error 39argument of type is not assignable to parameter of type39Stack Overflow
版权声明:本文标题:javascript - Typescript error 'argument of type is not assignable to parameter of type' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745233737a2648936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论