javascript - Typescript error 'argument of type is not assignable to parameter of type' - Stack Overflow

I'm relatively new to TS, and I'm running into a weird issue. I seem to have defined everythi

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

You 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[]])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745233738a4617790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信