首页 > 解决方案 > TS2345:类型的通用参数不可分配给 Typescript 4.4 中的类型参数

问题描述

由于我已经从 Typescript 版本更新4.04.4泛型,因此我遇到了一些错误,我无法修复它们。


const isFirstData = <TFirst, TSecond>(
    item: { type: ItemType.FIRST; data: TFirst } | { type: ItemType.SECOND; data: TSecond }
): item is { type: ItemType.FIRST; data: TFirst } => item.type === ItemType.FIRST;

const rows: ({
    type: ItemType.SECOND;
    data: TSecond;
} | {
    type: ItemType.FIRST;
    data: TFirst;
})[]; 

const item = rows[0];
const height = isFirstData(item) ? First?.value : Second?.value;

// Argument of type '{ type: ItemType.SECOND; data: TData; } | { type: ItemType.FIRST; data: TFirst; }' is not assignable to parameter of type '{ type: ItemType.FIRST; data: TData; } | { type: ItemType.SECOND; data: TData; }'.

有人可以解释打字稿发生了什么变化以及如何解决我的错误吗?

标签: javascripttypescriptgenerics

解决方案


推荐阅读