Promise<ConsumerStream> 不可分配给类型 Promise<void>
Promise<ConsumerStream> is not assignable to type Promise<void>
我想弄清楚为什么在构建过程中会出现此错误 (npm run build
):
ERROR: src/setup/kafka/index.ts:147:29 - error TS2322: Type ''.
Type 'ConsumerStream' is not assignable to type 'void'.
147 shutdown: () => Promise.resolve(stream.destroy()),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app.types.ts:26:15
26 shutdown: () => Promise<void>;
~~~~~~~~~~~~~~~~~~~
The expected type comes from the return type of this signature.
其中 stream
是一个 kafka.ConsumerStream
实例(参见 node-rdkafka
)
stream.destroy()
returnsvoid
,即destroy(error?: Error): void;
感谢您的回答
ConsumerStream.destroy
的 return 类型是 ConsumerStream
(请参阅此答案底部带有链接的类型详细信息)。您可以通过在 stream.destroy
的调用前加上 void
operator:
来重构您的方法
import {type ConsumerStream} from 'node-rdkafka';
declare const stream: ConsumerStream;
stream.destroy(); // ConsumerStream
type IncludesShutdown = {
shutdown: () => Promise<void>;
};
const problematic: IncludesShutdown = {
shutdown: async () => stream.destroy(), /*
^^^^^^^^^^^^^^^^
Type 'Promise<ConsumerStream>' is not assignable to type 'Promise<void>'.
Type 'ConsumerStream' is not assignable to type 'void'.(2322) */
};
const ok: IncludesShutdown = {
shutdown: async () => void stream.destroy(),
};
键入 ConsumerStream['destroy']
的详细信息:
https://github.com/Blizzard/node-rdkafka/blob/v2.12.0/index.d.ts#L137
export interface ConsumerStream extends Readable {
https://github.com/Blizzard/node-rdkafka/blob/v2.12.0/index.d.ts#L1
import { Readable, ReadableOptions, Writable, WritableOptions } from 'stream';
// declare module 'stream' {
// class Readable extends Stream implements NodeJS.ReadableStream {
destroy(error?: Error): this;
我想弄清楚为什么在构建过程中会出现此错误 (npm run build
):
ERROR: src/setup/kafka/index.ts:147:29 - error TS2322: Type ''.
Type 'ConsumerStream' is not assignable to type 'void'.
147 shutdown: () => Promise.resolve(stream.destroy()),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/app.types.ts:26:15
26 shutdown: () => Promise<void>;
~~~~~~~~~~~~~~~~~~~
The expected type comes from the return type of this signature.
其中 stream
是一个 kafka.ConsumerStream
实例(参见 node-rdkafka
)
stream.destroy()
returnsvoid
,即destroy(error?: Error): void;
感谢您的回答
ConsumerStream.destroy
的 return 类型是 ConsumerStream
(请参阅此答案底部带有链接的类型详细信息)。您可以通过在 stream.destroy
的调用前加上 void
operator:
import {type ConsumerStream} from 'node-rdkafka';
declare const stream: ConsumerStream;
stream.destroy(); // ConsumerStream
type IncludesShutdown = {
shutdown: () => Promise<void>;
};
const problematic: IncludesShutdown = {
shutdown: async () => stream.destroy(), /*
^^^^^^^^^^^^^^^^
Type 'Promise<ConsumerStream>' is not assignable to type 'Promise<void>'.
Type 'ConsumerStream' is not assignable to type 'void'.(2322) */
};
const ok: IncludesShutdown = {
shutdown: async () => void stream.destroy(),
};
键入 ConsumerStream['destroy']
的详细信息:
https://github.com/Blizzard/node-rdkafka/blob/v2.12.0/index.d.ts#L137
export interface ConsumerStream extends Readable {
https://github.com/Blizzard/node-rdkafka/blob/v2.12.0/index.d.ts#L1
import { Readable, ReadableOptions, Writable, WritableOptions } from 'stream';
// declare module 'stream' {
// class Readable extends Stream implements NodeJS.ReadableStream {
destroy(error?: Error): this;