【发布时间】:2018-05-19 07:10:28
【问题描述】:
是否可以从应用代码中的流对象类型定义中获取键(换句话说,流类型定义是否在运行时代码中以任何方式具体化)?
用例:
type Props = {
userID: string,
size: number | PhotoSize,
subscribePresence: Function,
unsubscribePresence: Function,
presenceStatus: ?PresenceStatus,
photoURL: ?string,
userName: ?string,
};
class Photo extends Component<Props> {
// ...
render() {
const { userID, size, presenceStatus } = this.props;
// Other props used elsewhere in the component
const restProps = _.omit(this.props, ???)
}
}
传播解构 (const { /* etc */ ... rest} = this.props) 不起作用,因为在 render 中没有使用其他道具。但是,我想选择其他可能已指定的道具(className、id 等)。
??? 可以派生自类似于Object.keys(Props) 的东西吗?据我所知,类型定义已被编译掉,因此尝试在运行时代码中引用 Props 会引发 RuntimeError: Props is not defined。
【问题讨论】:
-
不,所有类型信息在编译时被剥离
标签: javascript reactjs webpack flowtype