Answer the question
In order to leave comments, you need to log in
Typescript configuration error, how to fix?
Hello, I am learning rabbitmq broker using rascal library and nestjs.
By default, rabbit uses type topic, I wanted to change the value.
In nestjs I created a config taken from the cloudamqp site , only the values are changed, the library is in nestjs @types/rascal .
my RascalConfig file from which I get the error
{
"vhosts": {
"v2": {
"connection": {
"slashes": true,
"protocol": "amqp",
"hostname": "localhost",
"user": "smoke86",
"password": "smoke86",
"port": 5672,
"vhost": "v2",
"options": {
"heartbeat": 5
},
"socketOptions": {
"timeout": 10000
}
},
"exchanges": {
"user_ex": {
"type": "direct"
}
},
"queues": ["rabbit_q"],
"bindings": {
"v2h": {
"source": "user_ex",
"destination": "rabbit_q",
"destinationType": "queue",
"bindingKey": "uKey"
}
},
"publications": {
"user_pub": {
"vhost": "v2",
"exchange": "user_ex",
"routingKey": "uKey"
}
},
"subscriptions": {
"demo_sub": {
"queue": "rabbit_q",
"prefetch": 1
}
}
}
}
}
import { Injectable } from '@nestjs/common';
import { BrokerAsPromised } from 'rascal';
import RascalConfig from '../configs/rascal.config.json'
@Injectable()
export class RascalProvider {
private rascal = BrokerAsPromised;
async connectRascal(): Promise<BrokerAsPromised> {
return (await this.rascal.create(RascalConfig)) // Подчеркивает RascalConfog, ошибка тут
.on('error', console.error);
}
}
src/connect/rascal.provider.ts:10:42 - error TS2345: Argument of type '{ vhosts: { v2: { connection: { slashes: boolean; protocol: string; hostname: string; user: string; password: string; port: number; vhost: string; options: { heartbeat: number; }; socketOptions: { timeout: number; }; }; ... 4 more ...; subscriptions: { ...; }; }; }; }' is not assignable to parameter of type 'BrokerConfig'.
Types of property 'vhosts' are incompatible.
Type '{ v2: { connection: { slashes: boolean; protocol: string; hostname: string; user: string; password: string; port: number; vhost: string; options: { heartbeat: number; }; socketOptions: { timeout: number; }; }; ... 4 more ...; subscriptions: { ...; }; }; }' is not assignable to type '{ [key: string]: VhostConfig; }'.
Property '"v2"' is incompatible with index signature.
Type '{ connection: { slashes: boolean; protocol: string; hostname: string; user: string; password: string; port: number; vhost: string; options: { heartbeat: number; }; socketOptions: { timeout: number; }; }; ... 4 more ...; subscriptions: { ...; }; }' is not assignable to type 'VhostConfig'.
Types of property 'exchanges' are incompatible.
Type '{ user_ex: { type: string; }; }' is not assignable to type 'string[] | { [key: string]: ExchangeConfig; }'.
Type '{ user_ex: { type: string; }; }' is not assignable to type '{ [key: string]: ExchangeConfig; }'.
Property '"user_ex"' is incompatible with index signature.
Type '{ type: string; }' is not assignable to type 'ExchangeConfig'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"direct" | "fanout" | "headers" | "topic"'.
10 return (await this.rascal.create(RascalConfig))
"exchanges": ["user_ex"],
"bindings": ["user_ex[uKey] -> rabbit_q"]
I don't get any errors
Answer the question
In order to leave comments, you need to log in
It's all written in error. Direct import from .json infers an insufficiently strong type. The method expects that it type
will be strictly one of , but receives just .
You can do this:"direct" | "fanout" | "headers" | "topic"
string
The only reliable solution at the moment (until this feature is added ) is to keep the config in a .ts file in the form:
const RascalConfig: BrokerConfig = { ... }
or
Well, or write a simple generator of the corresponding .d.ts for each .json.
const RascalConfig = { ... } as const
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question