【问题标题】:Specifying the types of parameters in a function that takes 2 parameters在带有 2 个参数的函数中指定参数类型
【发布时间】:2022-11-25 21:52:22
【问题描述】:

我有一个带有两个参数的函数,如下所示。如果它们在对象中,我可以按如下方式指定它们的类型

    const renderItem = ({ item, index }: { item: string, index: number }) => {

        return (
            null
            )
    }

如果参数不在如下对象中,我如何指定它们的类型?

const renderItem = (item, index) => {

    return (
        null
    )
}

【问题讨论】:

    标签: reactjs typescript react-native


    【解决方案1】:

    您可以使用元组类型。这使得使用单一类型键入多个参数成为可能。

    const renderItem = (...[item, index]: [string, number]) => {}
    

    或者为每个参数赋予其自己的显式类型。

    const renderItem = (item: string, index: number) => {}
    

    Playground

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 2022-01-23
      • 2015-01-20
      • 2014-08-22
      相关资源
      最近更新 更多