简体   繁体   English

为 typescript 中的 useState 定义所需的类型

[英]Required type to be defined for useState in typescript

what is the required type to be defined for dataList in typescript please refer below snippet为 typescript 中的 dataList 定义的所需类型是什么,请参阅下面的代码片段

const [dataList, setDataList] = useState([]); 
      dataList= [
                 [{header:"first",tail:"three"},{header:"second",tail:"four"}], 
                 [{header:"first",tail:"three"},{header:"second",tail:"four"}] 
                ];

You will need to provide the types for the dataList state, as TypeScript is unable to infer it.您需要提供数据列表 state 的类型,因为dataList无法推断它。 This can be done via interfaces or type alias.这可以通过接口或类型别名来完成。

interface Data {
  header: string;
  tail: string; 
}

And this is how you can use it on the useState generic parameter, whereby dataList is a multidimensional array of the Data interface that is defined above:这就是您可以在useState泛型参数上使用它的方式,其中dataList是上面定义的Data接口的多维数组:

const [dataList, setDataList] = useState<Data[][]>([]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM