我需要在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
,但是它被弃用了。
如何修复它?
发布于 2020-06-17 21:29:42
kafka-node现在为Typescript提供了自己的预定义类型。你可以从here推荐他们
关于您的问题,在使用之前,您需要导入正确的类型:
import { KafkaClient as Client } from 'kafka-node';
const kafkaHost = 'localhost:9092';
const client = new Client({ kafkaHost });
发布于 2021-02-12 03:49:23
这是kafka-node中的一个报告错误,目前还没有官方解决方案。到目前为止,最好的答案是……不要让typescript检查kafka-node的源代码。
向tsconfig.json添加"skipLibCheck": true
https://stackoverflow.com/questions/62371643
复制相似问题