【发布时间】:2018-09-20 09:50:15
【问题描述】:
尝试使用流在反应组件上键入属性很有趣,其中属性可以是枚举或对象。
似乎当我使用{...spread} 作为可以具有多种类型(联合)的可选属性之后的属性时,流程总是指出它们不兼容。
这个想法是允许属性是来自枚举的字符串值,或者值的类型也必须是该枚举的对象。
如果将{...spread} 放在属性之前,它确实可以工作,但这不是所需的功能。
例如(Try Flow):
/* @flow */
import React from 'react';
type _TextAlign = 'left' | 'center' | 'right';
type TextAlign = _TextAlign | { [key: string]: _TextAlign };
type Props = { textAlign?: TextAlign }
const CStr = (props: Props) => <div textAlign="center" {...props} />;
给予
8: type Props = { textAlign?: TextAlign }
^ object type [1] is incompatible with string [2].
【问题讨论】:
-
我想很难理解你为什么要设置
textAlign = { 'a': 'right', 'b': 'center' }。看起来是个错误。
标签: javascript reactjs flowtype