为什么可以';t Typescript在使用&&;对对象中多个级别的键进行短路评估?

为什么可以';t Typescript在使用&&;对对象中多个级别的键进行短路评估?,typescript,Typescript,为什么下面的代码有问题,我正在测试一个有多个嵌套键的对象,在我测试下一个之前,它在每个级别上实际上都没有定义,但是TS说这个对象可能没有定义 有关测试为: chartAnnotations[ts] && chartAnnotations[ts][side] && chartAnnotations[ts][side][type] 接口事务{ prop1:字符串; prop2:数字 } 类型ChartTransaction=Pick 导出类型订单={ 状态:字符串;

为什么下面的代码有问题,我正在测试一个有多个嵌套键的对象,在我测试下一个之前,它在每个级别上实际上都没有定义,但是TS说这个对象可能没有定义

有关测试为:

chartAnnotations[ts] && chartAnnotations[ts][side] && chartAnnotations[ts][side][type]
接口事务{
prop1:字符串;
prop2:数字
}
类型ChartTransaction=Pick
导出类型订单={
状态:字符串;
类型:字符串;
}
导出接口ChartExecutionData{
已提交?:订单[];
打开?:订单[];
已执行?:订单[];
取消?:订单[];
是否部分执行?:订单[];
交易?:图表交易;
}
类型AnnotationObjectByType={[type in keyof ChartExecutionData]:string};
界面图表注释B侧{
购买?:注释ObjectByType;
销售?:AnnotationObjectByType;
}
界面图表注释{
[时间戳:编号]:ChartAnnotationsBySide;
}
常量chartAnnotations:chartAnnotations={}
类型sidesar=Array
const sides:sidesar=[“买入”,“卖出”]
类型TypesArr=数组
常量类型:TypesArr=['submitted']
常数ts:number=433242
侧面。forEach(侧面=>{
types.forEach(type=>{

if(图表注释[ts]&图表注释[ts][side]&图表注释[ts][side][type]){ } }) })
我已经创建了一个可以说明为什么这不起作用的程序

我不是100%确定,但我认为这是因为TypeScript在编译时不知道您试图使用键变量访问的属性是什么,所以不能确定该值是否未定义

编辑:若要为该答案添加解决方案,检查属性是否存在将在JavaScript中起作用,以便使TypeScript不会出现问题,您可以使用并告诉TS,您确信它不会在任何需要的地方被未定义。

我已经创建了一个解决方案,它可能会说明为什么这不起作用

我不是100%确定,但我认为这是因为TypeScript在编译时不知道您试图使用键变量访问的属性是什么,所以不能确定该值是否未定义


编辑:若要为该答案添加解决方案,检查属性是否存在将在JavaScript中起作用,以便使TypeScript不会出现问题,您可以使用并告诉TS,您确信它不会在任何需要的地方未定义。

这是TypeScript中的已知错误。无论出于何种原因,在具有索引签名的对象类型上读取非文字属性都不会作为该属性的索引。以下GitHub问题中似乎正在跟踪这一点:

  • “对字符串索引签名的属性访问应缩小”
  • this[variable]
    this[stringLiteral]
    具有不同的行为”

您可能希望转到这些问题并给它们一个这是TypeScript中的一个已知错误。无论出于何种原因,在具有索引签名的对象类型上读取非文字属性都不会作为该属性的索引。以下GitHub问题中似乎正在跟踪这一点:

  • “对字符串索引签名的属性访问应缩小”
  • this[variable]
    this[stringLiteral]
    具有不同的行为”

您可能想转到这些问题,并给他们一个您的意思是什么
chartAnnotations[ts]&&chartAnnotations[ts][type]&&chartAnnotations[ts][type][status]
我已经更新到我的意思了。抱歉,这个错误。看来短路的第二部分测试与第一部分相同,您想更深一层吗?chartAnnotations[ts]&&chartAnnotations[ts][side]&&chartAnnotations[ts][side][type]在该测试的最后一个元素中,它说chartAnnotations[ts][side]可能未定义。但是,我不是刚刚在&&你的意思是
chartAnnotations[ts]&&chartAnnotations[ts][type]&&chartAnnotations[ts][type][status]
的前一个元素中测试了这一点吗。抱歉,这个错误。看来短路的第二部分测试与第一部分相同,您想更深一层吗?chartAnnotations[ts]&&chartAnnotations[ts][side]&&chartAnnotations[ts][side][type]在该测试的最后一个元素中,它说chartAnnotations[ts][side]可能未定义。但我不是在前面的元素中测试过吗&&
interface Transaction {
  prop1: string;
  prop2: number
}

type ChartTransaction = Pick<Transaction, 'prop2'>

export type Order = {
  status: string;
  type: string;
}
export interface ChartExecutionData {
  submitted?: Order[];
  open?: Order[];
  executed?: Order[];
  canceled?: Order[];
  partiallyExecuted?: Order[];
  transaction?: ChartTransaction;
}
type AnnotationObjectByType = { [type in keyof ChartExecutionData]: string };

interface ChartAnnotationsBySide {
  buy?: AnnotationObjectByType;
  sell?: AnnotationObjectByType;
}

interface ChartAnnotations {
  [timestamp: number]: ChartAnnotationsBySide;
}

const chartAnnotations: ChartAnnotations = {}

type SidesArr = Array<keyof ChartAnnotationsBySide>

const sides: SidesArr = ['buy', 'sell']

type TypesArr = Array<keyof ChartExecutionData>

const types: TypesArr = ['submitted']

const ts: number = 433242
sides.forEach(side => {
  types.forEach(type => {
    if (chartAnnotations[ts] && chartAnnotations[ts][side] && chartAnnotations[ts][side][type]) {

    }
  })
})