如何在typescript下使用kafka节点?

xt0899hw  于 2021-06-05  发布在  Kafka
关注(0)|答案(1)|浏览(646)

我需要在typescript下使用kafka节点:

const kafkaHost = 'localhost:9092';
const client = new Client({ kafkaHost });

启动index.ts后,出现以下错误:

Property '_write' in type 'ProducerStream' is not assignable to the same property in base type 'Writable'.
  Type '(message: ProduceRequest, encoding: "utf8" | "buffer", cb: (error: any, data: any) => any) => void' is not assignable to type '(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null | undefined) => void) => void'.
    Types of parameters 'encoding' and 'encoding' are incompatible.
      Type 'BufferEncoding' is not assignable to type '"utf8" | "buffer"'.
        Type '"ascii"' is not assignable to type '"utf8" | "buffer"'.

143   _write (message: ProduceRequest, encoding: 'buffer' | 'utf8', cb: (error: any, data: any) => any): void;

我认为问题是在类型,所以我没有安装 @types/kafka-node 尽管如此,它还是被弃用了。
如何修复?

4c8rllxm

4c8rllxm1#

kafka节点现在为typescript提供了自己的预定义类型。你可以从这里介绍他们
关于您的问题,您需要在使用前导入正确的类型:

import { KafkaClient as Client } from 'kafka-node';

const kafkaHost = 'localhost:9092';
const client = new Client({ kafkaHost });

相关问题