打字稿类型HTTP请求REST API



所以,我的代码有一些问题,因为某些类型未指定,我不知道我应该使用哪种类型。

  const ***connectionOptions*** = {
    headers: {
      Authorization: 'Basic ' + new Buffer(this.config.USERNAME + ':' + this.config.PASSWORD).toString('base64')
    },
    host: 'http://lpr.houstonhidta.net',
    path: '/live/alarm',
    port: 8099
  };
  const ***request*** = http.request(connectionOptions, (***response***) => {
    response.setEncoding('utf8');
    response.on('data', (***chunk***) => {
      return response._read;
    });
  });```
Types missing for : <br/>
connectionOptions<br/>
request<br/>
response<br/>
chunk<br/>

我不知道您正在使用什么HTTP库,但通常存在以下选项:

  1. 您使用的库类型的库。在这种情况下,您可能只会潜入该库的来源并找到正确的类型。
  2. 类型是在@types/foo软件包中运送的(FOO是您正在使用的软件包的名称)。在这种情况下,您也可以通过阅读此软件包的来源找到类型。
  3. 都不可用,因此您需要提出自己的类型。这不是那么困难。

例如,您指定的第一个对象可能具有此类型:

type ConnectionOptions = {
  headers: {
    [s: string]: string
  },
  host: string,
  path: string,
  port: number
} 

最新更新