我需要在 typescript 下使用 kafka-node:
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
,但它已被弃用。如何解决?
最佳答案
kafka-node 现在带有它自己的 Typescript 预定义类型。您可以从 here 引用它们
关于您的问题,您需要在使用之前导入正确的类型:
import { KafkaClient as Client } from 'kafka-node';
const kafkaHost = 'localhost:9092';
const client = new Client({ kafkaHost });
关于node.js - 如何在typescript下使用kafka-node?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371643/