【发布时间】:2019-10-06 13:17:19
【问题描述】:
情况:
考虑让 myTypes 常量持有 prop-types(写在名为 my-component.js 的文件中的某处),如下所示:
import React from 'react'
import { View } from 'react-native'
import PropTypes from 'prop-types'
export const myTypes = {
activeColor: PropTypes.string,
color: PropTypes.string,
fontFamily: PropTypes.string,
fontSize: PropTypes.number,
fontWeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.number,
icon: PropTypes.node,
iconOverlay: PropTypes.node,
marginBottom: PropTypes.number,
marginLeft: PropTypes.number,
marginRight: PropTypes.number,
marginTop: PropTypes.number,
maxHeight: PropTypes.number,
minHeight: PropTypes.number,
onBlur: PropTypes.func,
onChangeText: PropTypes.func,
paddingBottom: PropTypes.number,
paddingLeft: PropTypes.number,
paddingRight: PropTypes.number,
paddingTop: PropTypes.number
}
export default class MyComponent extends React.Component {
static propTypes = myTypes
render () {
return (
<View></View>
);
}
}
您将如何使用myTypes 作为类型或助手来启用 IDE 自动完成功能?
我尝试的(在另一个用type-script 编写的文件中)如下:
import MyComponent, { myTypes } from 'my-component';
const dark_theme_properties: myTypes = {
activeColor: 'green'
};
当然,这会产生 'myTypes' refers to a value, but is being used as a type here. ts(2749) 错误,并且使用 typeof myTypes 也不会在 IDE 中提供正确的自动完成功能。
注意该组件是在
JavaScript ES6中编写的,而所需的自动完成功能应该在type-script中(其中导入了上述JS)。
【问题讨论】:
-
我觉得可以一一使用字段类型:Props { activeColor: myTypes['activeColor'] }
标签: javascript typescript react-native types jsdoc