根据TypeScript中的三元语句指定不同的类型

根据TypeScript中的三元语句指定不同的类型,typescript,Typescript,我有以下类型: selection: (InterfaceX | InterfaceY)[] interface InterfaceX { switch: boolean propertyOne: number } interface InterfaceY { switch: boolean propertyTwo: number } 选择可以是: {switch: true, propertyOne: 1} or {switch: false, propertyTwo:

我有以下类型:

selection: (InterfaceX | InterfaceY)[]

interface InterfaceX {
  switch: boolean
  propertyOne: number
}

interface InterfaceY {
  switch: boolean
  propertyTwo: number
}
选择可以是:

{switch: true, propertyOne: 1} or {switch: false, propertyTwo: 2}
这会导致三元数据中出现以下类型错误:

selection.switch ? selection.propertyOne : selection.propertyTwo

TS2339: Property 'propertyOne' does not exist on type 'InterfaceY'.
以及

TS2339: Property 'propertyTwo' does not exist on type 'InterfaceX'.
有没有办法声明三元结构的哪一侧使用哪种类型?因此,如果开关为真,则为
interfaceex
,否则为
InterfaceY
?我不想向接口添加任何属性。谢谢

新示例:

import React from "react";

export interface Props {
  items: (InterfaceX | InterfaceY)[];
}

interface InterfaceX {
  toggle: true;
  sharedProp: string;
  propertyX: string;
}

interface InterfaceY {
  toggle: false;
  sharedProp: string;
  propertyY: string;
}

export const Picks = (props: Props) => {
  const { items } = props;
  return items.map(item =>
    item.toggle ? (
      <div>
        {item.sharedProp} {item.propertyX} // Unresolved variable propertyX
      </div>
    ) : (
      <div>
        {item.sharedProp} {item.propertyY} // Unresolved variable propertyY
      </div>
    )
  );
};
从“React”导入React;
导出接口道具{
项目:(InterfaceX | InterfaceY)[];
}
接口接口{
切换:真;
sharedProp:字符串;
propertyX:字符串;
}
接口接口{
切换:假;
sharedProp:字符串;
propertyY:字符串;
}
导出常量拾取=(道具:道具)=>{
常量{items}=props;
返回items.map(item=>
item.toggle(
{item.sharedProp}{item.propertyX}//未解析变量propertyX
) : (
{item.sharedProp}{item.propertyY}//未解析变量propertyY
)
);
};

根据您的示例,您需要将
开关
属性设置为精确值(而不是
布尔值
),以便TypeScript可以正常运行。否则,由于它们可以是
false
true
,因此无法确定它是哪种类型

例如:

接口接口{
开关:真;
属性:编号;
}
接口接口{
开关:假;
属性2:编号;
}
//使用三元函数的示例
常数fn1=(选择:InterfaceX | InterfaceY)=>{
selection.switch?selection.propertyOne:selection.propertyTwo
}
//使用swtich的示例
常数fn2=(选择:InterfaceX | InterfaceY)=>{
开关(选择开关){
大小写正确:
返回selection.propertyOne;
案例错误:
返回selection.propertyTwo
}
}

根据您的示例,您需要将
开关
属性设置为精确值(而不是
布尔值
),以便TypeScript可以正常运行。否则,由于它们可以是
false
true
,因此无法确定它是哪种类型

例如:

接口接口{
开关:真;
属性:数字;
}
接口接口{
开关:假;
属性2:编号;
}
//使用三元函数的示例
常数fn1=(选择:InterfaceX | InterfaceY)=>{
selection.switch?selection.propertyOne:selection.propertyTwo
}
//使用swtich的示例
常数fn2=(选择:InterfaceX | InterfaceY)=>{
开关(选择开关){
大小写正确:
返回selection.propertyOne;
案例错误:
返回selection.propertyTwo
}
}

您是如何定义接口的?这似乎是可行的(假设您以这种方式定义接口):@skovy您在therehm中有true false而不是boolean,是的,根据您的示例,
选择
可以是
true
对于
InterfaceX
或true对于
InterfaceY
——如果没有此选项,TypeScript如何区分这两种类型?啊,也许我想的是错的,如果true是一种有效的类型,它实际上可以工作,我一直认为它必须是布尔型的,因为某种原因-将它张贴在下面,我将它标记为答案你是如何定义你的接口的?这似乎是可行的(假设您以这种方式定义接口):@skovy您在therehm中有true false而不是boolean,是的,根据您的示例,
选择
可以是
true
对于
InterfaceX
或true对于
InterfaceY
——如果没有此选项,TypeScript怎么能区分这两者呢?啊,也许我想错了,如果true是一个有效的类型,实际上可以工作,我一直认为它必须是布尔型的,因为某种原因-把它贴在下面,我会把它标记为答案啊,该死,我们必须在这里使用开关吗?我正在写一些JSX。将返回类似于
开关的内容?{s.propertyOne}:{s.propertyTwo}
。我试过了,但仍然得到一个三元的类型错误。你们应该能够使用三元或开关。最初测试时,我将两者都包括在内。三元应该在开关示例上方。我更新了示例,因为这对我不起作用。不幸的是,我得到了它,您必须添加(项目为InterfaceX),然后这就起作用了。非常感谢。啊,该死的,我们必须用开关吗?我正在写一些JSX。将返回类似于
开关的内容?{s.propertyOne}:{s.propertyTwo}
。我试过了,但仍然得到一个三元的类型错误。你们应该能够使用三元或开关。最初测试时,我将两者都包括在内。三元应该在开关示例上方。我更新了示例,因为这对我不起作用。不幸的是,我得到了它,您必须添加(项目为InterfaceX),然后这就起作用了。非常感谢。