英文:
Is it possible to compute type to react component props based on the presence or value of another prop using flow?
问题
我正在尝试根据另一个值来计算值的类型。如果 prop1 的类型是 x,那么 prop2 的类型应该是 y。
例如,在输入字段的情况下,
<Input
type="number"
onChange={(value: number | string) => {}}
/>
如果 prop 的 type="number"
,那么 onChange 中的 value 应该是 number
,否则应该是 string
。我基本上试图在这里移除 |
条件。
当我尝试这样调用 Input 时:
<Input
type="number"
onChange={(value: number) => {}}
/>
Flow 会抛出一个错误,因为如果使用 |
,value 可以是数字或字符串。在这种情况下,类型显然是 "number"
,有没有办法让 Flow 理解在这种情况下它将是数字?
Input 组件如下:
type CommonProps = {
disabled?: boolean,
autoFocus?: boolean,
placeholder: string
};
type PropsNumber = {
type: 'number',
onChange: (number) => void,
};
type PropsString = {
type: 'string' | 'email',
onChange: (string) => void,
};
type Props = CommonProps & (PropsNumber | PropsString);
class Input extends React.Component<Props, {}> {
render(){
return (
<input
onChange={this.props.onChange}
type={this.props.type}
disabled={this.props.disabled}
autoFocus={this.props.autoFocus}
/>
);
}
}
在调用此组件的任何地方都会得到错误消息 Could not decide which case to select, since case 1 [1] may work but if it doesn't case 2 [2] looks promising too.
。添加了一个playground以供参考。
英文:
I'm trying to compute the type of value based on another. If the type of prop1 is x then the type of prop2 should be y.
For example, in case of an input field,
<Input
type="number"
onChange={(value: number | string) => {}}
/>
if the prop type="number"
then typeof value from onChange should be number
and string
otherwise. I'm basically trying to remove the |
condition here.
When I try to call the Input like this:
<Input
type="number"
onChange={(value: number) => {}}
/>
Flow is throwing an error because the value can be either number or string if the |
is used. The value will obviously number in the case as the type is "number"
. Is there a way to make flow understand this will be number in this case?
The Input component looks like:
type CommonProps = {
disabled?: boolean,
autoFocus?: boolean,
placeholder: string
};
type PropsNumber = {
type: 'number',
onChange: (number) => void,
};
type PropsString = {
type: 'string' | 'email',
onChange: (string) => void,
};
type Props = CommonProps & (PropsNumber | PropsString);
class Input extends React.Component<Props, {}> {
render(){
return (
<input
onChange={this.props.onChange}
type={this.props.type}
disabled={this.props.disabled}
autoFocus={this.props.autoFocus}
/>
);
}
}
Getting the error Could not decide which case to select, since case 1 [1] may work but if it doesn't case 2 [2] looks promising too.
wherever this component is called.
Adding playground for the scenario.
答案1
得分: 2
解决这个问题的最佳方式是结合不交叉的联合和类型扩展来处理共同的属性:
import React from 'react';
type CommonProps = {
autoFocus?: boolean,
placeholder?: string,
};
type PropsNumber = {
type: "number",
onChange: (value: number) => void,
...CommonProps,
};
type PropsString = {
type: "string",
onChange: (value: string) => void,
...CommonProps,
};
type Props = PropsNumber | PropsString;
class Input extends React.Component<Props> {
render() {
if (this.props.type === "number") {
this.props.onChange(5);
} else {
this.props.onChange("foo");
}
(this.props.autoFocus: ?boolean);
// placeholder
return null;
}
}
<Input type="number" onChange={(value: number) => {}} />;
<Input type="string" onChange={(value: string) => {}} />;
// 预期的错误
<Input type="number" onChange={(value: string) => {}} />;
这段代码通过了Flow,除了在类型被指定为“number”但回调函数接受“string”类型参数时出现的预期错误。
不建议使用交叉类型来组合对象类型。相反,应该使用类型扩展,并且应该与精确类型一起使用。
请注意,在为了调用onChange
而细化props
的类型时,如果您做了比我这里的最小示例更复杂的事情,您可能会遇到细化失效。您可以通过将值提取到const
绑定中来解决这些问题。
英文:
The best way to solve this is with a combination of disjoint unions and type spread for the common props:
import React from 'react';
type CommonProps = {|
autoFocus?: boolean,
placeholder?: string,
|};
type PropsNumber = {
type: "number",
onChange: number => void,
...CommonProps,
};
type PropsString = {
type: "string",
onChange: string => void,
...CommonProps,
};
type Props = PropsNumber | PropsString;
class Input extends React.Component<Props> {
render() {
if (this.props.type === "number") {
this.props.onChange(5);
} else {
this.props.onChange("foo");
}
(this.props.autoFocus: ?boolean);
// placeholder
return null;
}
}
<Input type="number" onChange={(value: number) => {}}/>;
<Input type="string" onChange={(value: string) => {}}/>;
// Expected error
<Input type="number" onChange={(value: string) => {}}/>;
This passes Flow, except for the expected error where the type is specified as number
but the callback takes an argument of type string
.
Intersection types are not recommended as a way to combine object types. Instead, type spread should be used, and it should be used with exact types.
Note that when you are refining the type of props
in order to call onChange
, if you do something more complicated than my minimal example here you may run into refinement invalidations. You'll be able to work around these by pulling values out into const
bindings.
答案2
得分: -1
TypeScript的创建目的正是确定值的类型,如果它受到条件限制,它将失去其意义...
在您的情况下,您最多可以通过选择两种不同的函数来进行条件处理
onChange={
type === "number"
? (value: number) => {}
: (value: string) => {}
}
英文:
type script was created exactly to determine the type of value, if it will be conditional it will lose its sense...
In your case the most you can do is condition with choice of two different functions
onChange={
type === "number"
? (value: number) => {}
: (value: string) => {}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论