【问题标题】:What is the TypeScript declaration equivalent of PropTypes.arrayOf?PropTypes.arrayOf 的 TypeScript 声明等价物是什么?
【发布时间】:2020-07-29 10:43:26
【问题描述】:

我希望将 PropTypes 转换为 Typecript 声明。

我可以找到其中大多数的等价物,但我不知道这个:

fruits: PropTypes.arrayOf(PropTypes.oneOf(['apple', 'banana', 'cherry', 'dewberry']))

我最好的尝试是:

fruits: any[]

但它应该针对列出的项目。

以下可能是属性 fruits 的有效和无效值:

// Valid
fruits: ['apple']
fruits: ['cherry', 'dewberry']
// Invalid
fruits: ['my', 'little', 'pony']

感谢您的帮助!

【问题讨论】:

    标签: reactjs typescript typescript-typings react-props react-proptypes


    【解决方案1】:

    类似这样的:

    type fruit = 'apple' | 'banana' | 'cherry' | 'dewberry'
    
    type fruits = fruit[]
    
    const a:fruits = ['apple'] // ok
    const b:fruits = ['cherry', 'dewberry'] // ok
    const c:fruits = ['my', 'little', 'pony'] // error
    

    在TS playground 上试用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2014-06-12
      相关资源
      最近更新 更多