带有indexOf的打字稿无法正确评估联合类型?

时间:2020-10-06 09:58:59

标签: typescript

在下面的示例中,我使用联合类型的值之一。但是,当我尝试从ROW_ACTION查找数组中某个值的存在时。像Object.values(TABLE_COLUMN).indexOf(colType)一样,这使我出错。

Argument of type '"TEXT" | "ACTION_ITEM"' is not assignable to parameter of type 'TABLE_COLUMN'. Type '"TEXT"' is not assignable to type 'TABLE_COLUMN'.
enum ROW_ACTION {
    ACTION_ITEM = 'ACTION_ITEM'
}

enum TABLE_COLUMN {
    TEXT = 'TEXT'
}

const tableRows = {
    ...ROW_ACTION,
    ...TABLE_COLUMN
}

type columnTypeFromRow = keyof typeof tableRows;
const colType = 'TEXT' as columnTypeFromRow;
console.log("values ", Object.values(TABLE_COLUMN))
console.log(Object.values(TABLE_COLUMN).indexOf(colType)) // Argument of type '"TEXT" | "ACTION_ITEM"' is not assignable to parameter of type 'TABLE_COLUMN'. Type '"TEXT"' is not assignable to type 'TABLE_COLUMN'.

我尝试使用indexOf查找的值存在于数组中,并且应该满足打字稿键入的要求。但是还是错误。

here是指向IDE的链接,在该链接上会重现错误

0 个答案:

没有答案