我希望将PropTypes转换为Typecript声明。
我可以找到其中大多数的等效项,但是我不知道这一点:
fruits: PropTypes.arrayOf(PropTypes.oneOf(['apple', 'banana', 'cherry', 'dewberry']))
我最大的尝试是:
fruits: any[]
但是它应针对所列项目。
以下值可能是属性水果的有效值和无效值:
// Valid
fruits: ['apple']
fruits: ['cherry', 'dewberry']
// Invalid
fruits: ['my', 'little', 'pony']
谢谢您的帮助!
type
:type FRUITS = 'apple' | 'banana' | 'cherry' | 'dewberry'
然后您可以像这样设置fruits
的类型:
fruits: FRUITS[]
这意味着fruits
只能是类型为FRUITS
的数组。
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上试用。