Open devcitopia opened 1 year ago
We just created a PR related to this issue: https://github.com/moleculerjs/moleculer-channels/pull/54
Just to add here, typescript support seems quite broken to the point I have to use the any
keyword in my TS code to use this package, and that defeats the purpose of Typescript.
Here is my sample typescript code:
index.ts:
import { ServiceBroker } from 'moleculer';
import { Middleware } from '@moleculer/channels';
const main = async () => {
// service schema
const serviceSchema = {
name: 'helper',
channels: {
async 'helper.sum'(payload: { a: number; b: number }): Promise<number> {
return payload.a + payload.b;
},
},
};
const mw = Middleware({ adapter: { type: 'Fake' } }); // ERROR here
const broker = new ServiceBroker({
logLevel: 'info',
middlewares: [mw], // ERROR here
});
broker.createService(serviceSchema);
await broker.start();
await broker.stop();
};
main();
Running npx tsc
, I get these errors:
npx tsc
index.ts:16:27 - error TS2322: Type '{ type: string; }' is not assignable to type 'string | AdapterConfig'.
Property 'options' is missing in type '{ type: string; }' but required in type 'AdapterConfig'.
16 const mw = Middleware({ adapter: { type: 'Fake' } });
~~~~~~~
node_modules/@moleculer/channels/types/src/index.d.ts:130:5
130 options: import("./adapters/base").BaseDefaultOptions & import("./adapters/amqp").AmqpDefaultOptions & import("./adapters/kafka").KafkaDefaultOptions & import("./adapters/nats").NatsDefaultOptions & import("./adapters/redis").RedisDefaultOptions;
~~~~~~~
'options' is declared here.
node_modules/@moleculer/channels/types/src/index.d.ts:139:5
139 adapter: string | AdapterConfig;
~~~~~~~
The expected type comes from property 'adapter' which is declared here on type 'MiddlewareOptions'
index.ts:20:19 - error TS2322: Type '{ name: string; created(_broker: ServiceBroker): void; serviceCreated(svc: Service<ServiceSettingSchema>): Promise<void>; serviceStopping(svc: Service<...>): Promise<...>; started(): Promise<...>; stopped(): Promise<...>; }' is not assignable to type 'string | Middleware'.
Type '{ name: string; created(_broker: ServiceBroker): void; serviceCreated(svc: Service<ServiceSettingSchema>): Promise<void>; serviceStopping(svc: Service<...>): Promise<...>; started(): Promise<...>; stopped(): Promise<...>; }' is not assignable to type 'Middleware'.
Property 'name' is incompatible with index signature.
Type 'string' is not assignable to type '((handler: ActionHandler<any>, action: ActionSchema) => any) | ((handler: ActionHandler<any>, event: ServiceEvent) => any) | ((handler: ActionHandler<...>) => any) | ((service: Service<...>) => any) | ((broker: ServiceBroker) => any) | ((handler: CallMiddlewareHandler) => CallMiddlewareHandler)'.
20 middlewares: [mw],
~~
Found 2 errors in the same file, starting at: index.ts:16
I can make this work by using the any
keyword in setting the options for the middleware, and also while adding the middleware:
index.ts:
import { ServiceBroker } from 'moleculer';
import { Middleware } from '@moleculer/channels';
const main = async () => {
// service schema
const serviceSchema = {
name: 'helper',
channels: {
async 'helper.sum'(payload: { a: number; b: number }): Promise<number> {
return payload.a + payload.b;
},
},
};
const mw = Middleware({ adapter: { type: 'Fake' } } as any); // I should not need to use 'as any' here, this is due to the typings marking all fields as mandatory
const broker = new ServiceBroker({
logLevel: 'info',
middlewares: [mw as any], // I should not need to use 'as any' here, this is due to mismatch in typings between what is expected and and what is supplied
});
broker.createService(serviceSchema);
await broker.start();
await broker.stop();
};
main();
And I have to set "skipLibCheck": true
for it to compile, otherwise, it checks all the `*.d.ts
files and gets errors for the brokers that are not being used. I use AMQP, so I get errors for every other broker since they are not installed:
npx tsc
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:113:27 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
113 type KafkaClient = import('kafkajs').Kafka;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:117:29 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
117 type KafkaProducer = import('kafkajs').Producer;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:121:29 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
121 type KafkaConsumer = import('kafkajs').Consumer;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:129:34 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
129 type EachMessagePayload = import('kafkajs').EachMessagePayload;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:133:27 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
133 type KafkaConfig = import('kafkajs').KafkaConfig;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:137:30 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
137 type ProducerConfig = import('kafkajs').ProducerConfig;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/kafka.d.ts:141:30 - error TS2307: Cannot find module 'kafkajs' or its corresponding type declarations.
141 type ConsumerConfig = import('kafkajs').ConsumerConfig;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:63:56 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
63 createConsumerHandler(chan: Channel): (err: import("nats").NatsError, message: JsMsg) => Promise<void>;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:73:105 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
73 createStream(streamName: string, subjects: Array<string>, streamOpts: StreamConfig): Promise<import("nats").StreamInfo>;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:120:30 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
120 type NatsConnection = import("nats").NatsConnection;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:124:32 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
124 type JetStreamManager = import("nats").JetStreamManager;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:128:31 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
128 type JetStreamClient = import("nats").JetStreamClient;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:132:37 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
132 type JetStreamSubscription = import("nats").JetStreamSubscription;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:140:21 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
140 type JsMsg = import("nats").JsMsg;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:144:28 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
144 type StreamConfig = import("nats").StreamConfig;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:148:39 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
148 type JetStreamPublishOptions = import("nats").JetStreamPublishOptions;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:152:33 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
152 type ConnectionOptions = import("nats").ConnectionOptions;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:156:35 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
156 type ConsumerOptsBuilder = import("nats").ConsumerOptsBuilder;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:160:28 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
160 type ConsumerOpts = import("nats").ConsumerOpts;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:164:32 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
164 type JetStreamOptions = import("nats").JetStreamOptions;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/nats.d.ts:168:23 - error TS2307: Cannot find module 'nats' or its corresponding type declarations.
168 type MsgHdrs = import("nats").MsgHdrs;
~~~~~~
node_modules/@moleculer/channels/types/src/adapters/redis.d.ts:125:23 - error TS2307: Cannot find module 'ioredis' or its corresponding type declarations.
125 type Cluster = import("ioredis").Cluster;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/redis.d.ts:177:21 - error TS2307: Cannot find module 'ioredis' or its corresponding type declarations.
177 type Redis = import("ioredis").Redis;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/adapters/redis.d.ts:178:28 - error TS2307: Cannot find module 'ioredis' or its corresponding type declarations.
178 type RedisOptions = import("ioredis").RedisOptions;
~~~~~~~~~
node_modules/@moleculer/channels/types/src/index.d.ts:30:1 - error TS2309: An export assignment cannot be used in a module with other exported elements.
30 export = _exports;
I think typescript support needs to be seriously revisited here...
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Current Behavior
I try to tsc my project using @moleculer/channels but it returns me two errors:
@moleculer/channels/types/src/adapters/base.d.ts:120:35 - error TS2304: Cannot find name 'Service'.
@moleculer/channels/types/src/index.d.ts:30:1 - error TS2309: An export assignment cannot be used in a module with other exported elements.
The first error coming from version 0.1.3
Expected Behavior
I can compile my project.
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
Context
Proposing to change
export = _exports
toexport default _exports
in types/src/index.d.ts. And add import of type Service in types/src/adapters/base.d.ts (in subscription function declaration)