是否可以将对象中函数的流类型动态设置为其属性之一的类型?
Is it possible to dynamically set flow type of a function in an object to the type of one of its properties?
考虑以下类型的配置:
type Config = {
props: any,
isEnabled?: (args: { custom: string, props: any }) => boolean,
};
我想使用这个 Config type
这样对它的任何更改都会应用到它的所有子类型。我可以这样做:
type ConfigAProps = {
propA: boolean
}
type ConfigBProps = {|
propsB: string
|}
type ConfigA = {|
...Config,
props: ConfigAProps
|}
type ConfigB = {|
...Config,
props: ConfigBProps
|}
现在 type Config
中的 isEnabled
仍然有参数 props:any
。有没有办法指定 isEnabled 中的 props 类型与对象中的 props 字段类型相同(有点像 this.props)?
或者有没有更好的方法来模拟这些类型?
我想你想在这里使用泛型。而不是 splatting Config
,而是:
type Config<T> = {
props: T,
isEnabled?: (args: { custom: string, props: T }) => boolean,
};
type ConfigA = Config<ConfigAProps>;
考虑以下类型的配置:
type Config = {
props: any,
isEnabled?: (args: { custom: string, props: any }) => boolean,
};
我想使用这个 Config type
这样对它的任何更改都会应用到它的所有子类型。我可以这样做:
type ConfigAProps = {
propA: boolean
}
type ConfigBProps = {|
propsB: string
|}
type ConfigA = {|
...Config,
props: ConfigAProps
|}
type ConfigB = {|
...Config,
props: ConfigBProps
|}
现在 type Config
中的 isEnabled
仍然有参数 props:any
。有没有办法指定 isEnabled 中的 props 类型与对象中的 props 字段类型相同(有点像 this.props)?
或者有没有更好的方法来模拟这些类型?
我想你想在这里使用泛型。而不是 splatting Config
,而是:
type Config<T> = {
props: T,
isEnabled?: (args: { custom: string, props: T }) => boolean,
};
type ConfigA = Config<ConfigAProps>;