bulletproof-nodejs icon indicating copy to clipboard operation
bulletproof-nodejs copied to clipboard

TS error Logger.error

Open hubochkin opened this issue 4 years ago • 7 comments

src/subscribers/user.ts:29:14 - error TS2339: Property 'error' does not exist on type 'unknown'


import { Container } from 'typedi';

const Logger = Container.get('logger');
Logger.error(`🔥 Error on event ${events.user.signIn}: %o`, e);

But if I do the next code - everything is fine

import Logger from '../loaders/logger';
Logger.error(`🔥 Error on event ${events.user.signIn}: %o`, e);

As I can see. typedi do not see levels in Container. How can I fix this ?

hubochkin avatar Nov 11 '19 15:11 hubochkin

  • Are you registering the logger within typedi?
  • Are you calling the event subscriber loader before the container's initialization?

santiq avatar Nov 11 '19 17:11 santiq

@santiq Container.set('logger', LoggerInstance) in dependencyInjector

But it just a copy of this git project. I cant build a project.

Can we fix this together?

hubochkin avatar Nov 12 '19 11:11 hubochkin

@santiq I'm having the same issue here. @hubochkin Were you able to fix this issue?

peiris avatar Nov 26 '19 12:11 peiris

@peiris No, I didn't :(

hubochkin avatar Nov 27 '19 11:11 hubochkin

I got this to work by adding a LoggerInterface in src/interfaces/LoggerInterface.ts:

interface LoggerInterface {
  debug(message: string, ...args: any[]): void;
  info(message: string, ...args: any[]): void;
  warn(message: string, ...args: any[]): void;
  error(message: string, ...args: any[]): void;
  silly(message: string, ...args: any[]): void;
}

and then adding that type to every definition of the logger container: const logger: LoggerInterface = Container.get('logger');

I am extremely new to TS so there is probably a better way to do this but it does appear to work.

mattlehrer avatar Dec 10 '19 22:12 mattlehrer

This looks to me to be a duplicate of https://github.com/santiq/bulletproof-nodejs/issues/27

bjfletcher avatar Dec 21 '19 17:12 bjfletcher

I got this to work by specifying the generic type as winston.Logger

const logger = Container.get<winston.Logger>('logger');

lechodiman avatar Dec 22 '19 19:12 lechodiman