typecript 承诺返回类型异步函数

时间:2021-03-19 15:08:20

标签: typescript

我有这个异步函数,正在尝试返回一个对象或 null。

但我在定义类型时出错。

如何定义这种数据类型?

const checkIsValidConnection = async (number: string): Promise<string | null> => {
  const defaultConnection = await GetDefaultConnection();

  const wbot = getWbot(defaultConnection.id);

  const contactId = await wbot.getNumberId(`${number}`);

  return contactId;
};

export default checkIsValidConnection;
<块引用>

输入'ContactId | null' 不能分配给类型 'string | null'.
类型“ContactId”不可分配给类型“string”。

enter image description here

1 个答案:

答案 0 :(得分:2)

我相信您正在与 https://github.com/pedroslopez/whatsapp-web.js/blob/master/index.d.ts

表示 WAWEBJS.ContactID 为以下接口。

interface ContactId { server: string, user: string, _serialized: string}

您应该将承诺的返回类型更新为此 interface 或坚持 WAWEBJS.ContactID

相关问题