Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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
Typescript TS2339:Array.find在已识别的并集中未正确推断类型_Typescript - Fatal编程技术网

Typescript TS2339:Array.find在已识别的并集中未正确推断类型

Typescript TS2339:Array.find在已识别的并集中未正确推断类型,typescript,Typescript,在这个例子中,我有一个购物车,它可以装满不同种类的物品,在这个例子中,它的高尔夫球和高尔夫俱乐部有自己的选择 我得到以下代码错误: TS2339:类型“{color:“blue”|“red”|“white”}{variant:“wedge”|“putter”}”上不存在属性“color”。 类型“{variant:“wedge”|“putter”}”上不存在属性“color”。 类型ProductGolfBall={ 类型:“高尔夫球”; 选项:{ 颜色:“蓝色”|“红色”|“白色”; };

在这个例子中,我有一个购物车,它可以装满不同种类的物品,在这个例子中,它的高尔夫球和高尔夫俱乐部有自己的选择

我得到以下代码错误:

TS2339:类型“{color:“blue”|“red”|“white”}{variant:“wedge”|“putter”}”上不存在属性“color”。
类型“{variant:“wedge”|“putter”}”上不存在属性“color”。
类型ProductGolfBall={
类型:“高尔夫球”;
选项:{
颜色:“蓝色”|“红色”|“白色”;
};
};
类型ProductGolfClub={
类型:“高尔夫俱乐部”;
选项:{
变体:“楔子”|“推杆”;
};
};
类型CartItem=ProductGolfBall | ProductGolfClub;
类型CartItems=数组;
常量购物车:购物车项目=[
{
类型:“高尔夫球”,
选项:{
颜色:“蓝色”
}
},
{
类型:“高尔夫俱乐部”,
选项:{
变体:“楔形”
}
},
{
类型:“高尔夫俱乐部”,
选项:{
变体:“推杆”
}
}
];
const golfball=cart.find((项目)=>item.type==“golfball”);
如果(高尔夫球){//检查它是否真实
//根据typescript,这仍然可以是ProductGolfClub或ProductGolfBall
console.log(golfball.type)
console.log(golfball.options.color)//产生上面的TS2339错误
}
现在我不明白为什么当find操作仅对
type
属性为
golfball
的数组项返回true时,
golfball
变量仍然可以是
ProductGolfClub


我可以将
golfball
变量设置为
ProductGolfBall
,但必须有其他方法让typescript了解变量的类型。

不幸的是,TS无法从这个箱子中取出类型防护装置。 但您可以通过明确说明函数将确保什么来手动执行,请考虑:

函数为高尔夫球(项目:CartItem):项目为ProductGolfBall{
return item.type==“高尔夫球”;
}
const golfball=cart.find(isGolfBall)
如果(高尔夫球){//检查它是否真实
console.log(golfball.type)

console.log(golfball.options.color)//我看不出有错误。这似乎是调用的。既然TS不能为这种情况自动生成类型保护,那么有没有其他方法来编写此代码,而不需要用户定义的类型保护?我不这么认为。一般来说,我看到了一种可读且正确的方法来实现您所需要的。然后从现在开始,我将使用
变量is type
继续,谢谢你的帮助,麦基!
type ProductGolfBall = {
  type: "golfball";
  options: {
    color: "blue" | "red" | "white";
  };
};

type ProductGolfClub = {
  type: "golfclub";
  options: {
    variant: "wedge" | "putter";
  };
};

type CartItem = ProductGolfBall | ProductGolfClub;
type CartItems = Array<CartItem>;

const cart: CartItems = [
  {
    type: "golfball",
    options: {
      color: "blue"
    }
  },
  {
    type: "golfclub",
    options: {
      variant: "wedge"
    }
  },
  {
    type: "golfclub",
    options: {
      variant: "putter"
    }
  }
];

const golfball = cart.find((item) => item.type === "golfball");

if (golfball) { // Check that it's truthy
  // According to typescript this can still be either ProductGolfClub or ProductGolfBall
  console.log(golfball.type)
  console.log(golfball.options.color) // Produces the TS2339 error above
}