javascript - Typescript - 基于列表中的值的条件类型
问题描述
所以这些是我的类型:
export type typeOne = {
A: string;
B: string;
};
export type typeTwo = {
A: string;
C: string;
};
export type Result = typeOne | typeTwo; //condition
这是用例:
for (const item: Result of list) {
const something = item.B ? 'B exsist' : item.C;
return something;
}
它不断返回错误:
TS2339:“结果”类型上不存在属性“B”。类型“typeTwo”上不存在属性“B”。
还有另一个:
TS2339:“结果”类型上不存在属性“C”。类型“typeOne”上不存在属性“C”。
你知道我该如何解决这个问题吗?
注意:循环也发生以下错误:
TS2483:“for...of”语句的左侧不能使用类型注释。