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

getting Property 'error' does not exist on type '{}'

Open lakshitaverma opened this issue 5 years ago • 7 comments

When running npm run build, getting Property 'error' does not exist on type '{}' in src/api/middlewares/attachCurrentUser.ts on Logger. Why?

lakshitaverma avatar Aug 04 '19 06:08 lakshitaverma

We need to do JSON.parse(Container.get('logger')) to remove error. It is in many files. Any generic way to solve this problem?

lakshitaverma avatar Aug 04 '19 07:08 lakshitaverma

Hi! Let me investigate this and will get back to you

santiq avatar Aug 04 '19 10:08 santiq

Instead of directly importing this - import { Container } from 'typedi'; we can create a wrapper over this and that wrapper will return us the parsed version of object. What do you say?

lakshitaverma avatar Aug 04 '19 11:08 lakshitaverma

When you get a service from DI container you should define the returned value as Logger interface from winston.

import winston from 'winston';

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

dzianisreznik avatar Sep 18 '19 18:09 dzianisreznik

What's the update on this?

peiris avatar Nov 26 '19 12:11 peiris

Similar to @dzianisreznik's answer:

import { Logger } from 'winston';

const logger: Logger = Container.get('logger');

bjfletcher avatar Dec 21 '19 17:12 bjfletcher

@dzianisreznik and @bjfletcher this approach solves the problem.

Though without importing Logger, I used @ts-ignore like the following and it also resolved the problem though it was not neat and I don't like it.

const logger = Container.get("logger");
// @ts-ignore
logger.info("Breaking Log");

Now with your solutions, I was wondering if I'm getting the instance from Container then are we using import { Logger } from 'winston';

just to tell typescript that the instance we are pulling out from the DI container is of type Logger?

And does not import { Logger } from 'winston'; this line also import Logger codes from winston? Does not this increase the file size when transpiling?

SamsadSajid avatar Sep 26 '20 05:09 SamsadSajid