Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Typescript,基于字符串文字类型值的条件类型_Reactjs_Typescript_Redux - Fatal编程技术网

Reactjs Typescript,基于字符串文字类型值的条件类型

Reactjs Typescript,基于字符串文字类型值的条件类型,reactjs,typescript,redux,Reactjs,Typescript,Redux,我对如何基于预定义的字符串文本为typescript定义条件类型有点困惑 这是一个模型/示例,有助于解释对于一个小背景,用户应该标记来自图像的数据,目前只有两种类型的东西需要标记(例如,这个示例让我们说它的行人和车辆) 在我的Redux存储中,我存储来自API请求的数据(请求获取一组新的数据,根据这些类型之一进行标记)。问题是,我得到的数据格式不同,这两种类型的东西用帧数组的值来标记帧数组或对象 type ILabelingTypes = "pedestrians" | "vehicles"

我对如何基于预定义的字符串文本为typescript定义条件类型有点困惑

这是一个模型/示例,有助于解释对于一个小背景,用户应该标记来自图像的数据,目前只有两种类型的东西需要标记(例如,这个示例让我们说它的
行人
车辆

在我的Redux存储中,我存储来自API请求的数据(请求获取一组新的数据,根据这些类型之一进行标记)。问题是,我得到的数据格式不同,这两种类型的东西用帧数组的值来标记帧数组或对象

type ILabelingTypes = "pedestrians" | "vehicles"
框架应该是这样的

interface IFrame {
    url: string
    orientation: number
    // anything else related to a frame
}
api请求将是
/labeling/vehicles/new
,并附有响应示例

{
    // meta info about the labeling session
    frames: IFrame[]
}
/labeling/walkers/new
,带有示例响应

{
    // meta info about the labeling session
    frames: Map<string, IFrame[]>
}
也就是说,当我要呈现或使用这些数据时,我只想调用我知道存在的方法,具体取决于存储中定义的
labelingType


对于React组件端,当我去渲染帧时,我构建了一个要完成的帧队列,因此我需要知道各个组件中的数据是什么类型的(这是我想做的,而不是检查帧的类型)


您可以创建有区别的联合:

type ILabelingStore = ({
        labelingType: "vehicles",
        frames: IFrame[]
    } | {
        labelingType: "pedestrians",
        frames: { [key: string]: IFrame[] }
    });

感谢您的快速回复!这看起来像我需要的,但我的redux存储中有一些编译器错误(返回值不匹配)。解决这些问题后,如果这解决了我的问题,我会接受它!:)我在将字符串文字类型分配给受歧视的联合时遇到问题。介意看一下吗?编译器无法证明
有效载荷
与您的对象的值相同,因此它会给您一个错误,因为它担心属性不再匹配。那么我该如何证明呢?我已经指定了
有效负载
是这些值之一。我需要做一个防弹衣吗?我尝试了TypeGuard,但没有成功:/I我认为您需要将lambda设置为泛型,以便可以指定类型系统中的
ILabelingTypes
。然后,它将只接受与其类型参数匹配的任何参数。
// pedestrians component
componentDidMount() {
    Object.entries(this.props.frames).forEach( (key, val) => this.queue.concat(val))
}

// vehicles component
componentDidMount() {
    this.queue.concat(this.props.frames)
}
type ILabelingStore = ({
        labelingType: "vehicles",
        frames: IFrame[]
    } | {
        labelingType: "pedestrians",
        frames: { [key: string]: IFrame[] }
    });