不可分配给“字符串”类型。类型“ void”不可分配给类型“ string”

时间:2019-03-30 23:41:30

标签: typescript

我正在认真学习ts,诺言有什么问题? :D

以下功能给我这个错误

  

键入“字符串|无效”不能分配给“字符串”类型。输入“ void”   不可分配给“字符串”类型。

export async function getType(serviceID: string, vendor: string, {inventory = defaultKeyinventory } = {}): Promise<string | null>{

    const item= inventory !.getItem(serviceID, vendor)
        .catch(() => console.log('error'));

    return item;
}

1 个答案:

答案 0 :(得分:1)

.catch返回一个Promise,但是您需要在catch块中返回一些内容。您需要返回字符串或null。

export async function getType(
    serviceID: string, 
    vendor: string, 
    { inventory = defaultKeyinventory } = {}
): Promise<string | null>{

    const item= inventory !.getItem(serviceID, vendor)
        .catch(() => return null);

    return item;
}