类型'Isms91Options'的参数不能分配给类型'string |的参数。网址”

时间:2019-01-09 08:00:50

标签: node.js typescript http interface

我是Typescript的新手,只是尝试将Msg91 NodeJS API代码转换为Typescript,但是在编译代码时出错。即“类型为“ Isms91Options”的参数不能分配给类型为“字符串| URL”的参数。”

我试图消除所有可能的错误类型,但是我想我错过了一些。

interface IoptionsType {
    phoneNumber: string;
    message: string;
}

interface IheadersType {
  authkey: string;
  contentType: string;
}

interface Isms91Options {
    method: string;
    hostname: string;
    port: string;
    path: string;
    headers: IheadersType;
}

export const sendSms = (data: IoptionsType) => {
  const {message, phoneNumber} = data;
  const option: Isms91Options = {
                  headers: {
                    authkey: "XXXXXXXXXXXXX",
                    contentType: "application/json"
                  },
                  hostname: "api.msg91.com",
                  method: "POST",
                  path: `http://api.msg91.com/api/v2/sendsms?message=${message}&authkey=XXXXXXXXXXXXXXX&`
                        + `mobiles=+91${phoneNumber}&route=4&sender=XXXXX&country=91&unicode=1`,
                  port: null,
  };

  const req = http.request(option, (res: Response) => {
    const chunks: any[] = [];
    res.on("data", (chunk) => {
      chunks.push(chunk);
    });

    res.on("end", () => {
      log.info(Buffer.concat(chunks));
    });
  });

  req.write(JSON.stringify({
    country: "91",
    route: "4",
    sender: "XXXXXX",
    sms:
     [ { message: "testing", to: [ "XXXXXXX" ] } ] }));
  req.end();
};

这是我在编译代码时遇到的错误 错误TS2345:类型'Isms91Options'的参数不能分配给类型'字符串|的参数。网址”。   类型“ Isms91Options”缺少类型“ URL”中的以下属性:哈希,主机,href,原始和另外7个。

0 个答案:

没有答案