L
L
legacy_js2022-01-25 15:36:24
JSON
legacy_js, 2022-01-25 15:36:24

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

spoiler
{
  "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
        }
      }
    }
  }
}


RascalProvider.ts
spoiler

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);
  }
}



In another microservice, I do not use nestjs, I use rascal and express there and the same rascal config, and everything works fine there, I don’t get any error. I assume that the problem is in TypeScript, but how to fix it, I could not find a solution

Error code
spoiler
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))


And I also noticed if you change "exchanges and bindings" to
"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

1 answer(s)
A
Aetae, 2022-01-25
@legacy_js

It's all written in error. Direct import from .json infers an insufficiently strong type. The method expects that it typewill 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 question

Ask a Question

731 491 924 answers to any question