Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript errors creating custom transport #1382

Closed
1 of 2 tasks
chadjaros opened this issue Jun 28, 2018 · 3 comments
Closed
1 of 2 tasks

Typescript errors creating custom transport #1382

chadjaros opened this issue Jun 28, 2018 · 3 comments

Comments

@chadjaros
Copy link

chadjaros commented Jun 28, 2018

Please tell us about your environment:

  • winston version? 3.0.0
    • winston@2
    • winston@3
  • node -v outputs: v8.9.4
  • Operating System? macOS
  • Language? TypeScript 2.9.2

What is the problem?

I'm trying to declare a new custom Transport.

import Transport from 'winston-transport';

export class CustomLogger extends Transport {
  constructor(opts: any) {
    super(opts);
  }
  log(info, callback) {
    setImmediate(() => {
      this.emit('logged', info);
    });

    // Log Stuff

    if (callback) {
      callback();
    }
  }
}

I am seeing 2 compiler errors:

From import Transport from 'winston-transport';

error TS1192: Module '".../node_modules/winston-transport/index"' has no default export.

In 3.0.0-rc5 this could be worked around by doing

import * as winston from 'winston';

class Foo extends winston.Transport {
...

From this.emit('logged', info);

error TS2339: Property 'emit' does not exist on type 'AirbrakeLogger'.

I can work around this by doing (this as any).emit('logged', info);, but it's not pretty.

What do you expect to happen instead?

No compiler errors?

Other information

@DABH
Copy link
Contributor

DABH commented Jun 29, 2018

If I take your example, npm install winston-transport, and import * as Transport from winston-transport, all seems to work well. Let me know if that doesn't work for you or if you have other issues. Thanks!

@DABH DABH closed this as completed Jul 2, 2018
@opensas
Copy link

opensas commented Jun 14, 2019

I'm facing a similar issue here.

My code:

import * as Transport from 'winston-transport'
// import Transport from 'winston-transport';

export class CustomLogger extends Transport {
  constructor(opts: any) {
    super(opts);
  }
  log(info, callback) {
    setImmediate(() => {
      this.emit('logged', info);
    });

    // Log Stuff
    if (callback) {
      callback();
    }
  }
}

and i get:

lib/CustomLogger.ts:4:35 - error TS2507: Type 'typeof TransportStream' is not a constructor function type.

4 export class CustomLogger extends Transport {

lib/CustomLogger.ts:8:7 - error TS7006: Parameter 'info' implicitly has an 'any' type.

8   log(info, callback) {

lib/CustomLogger.ts:8:13 - error TS7006: Parameter 'callback' implicitly has an 'any' type.

8   log(info, callback) {

lib/CustomLogger.ts:10:12 - error TS2339: Property 'emit' does not exist on type 'CustomLogger'.

10       this.emit('logged', info);

Found 4 errors.

using:

  "dependencies": {
    "winston": "^3.2.1",
    "winston-transport": "^4.3.0"
  },
  "devDependencies": {
    "@types/node": "^12.0.7",
    "typescript": "^3.5.1"
  }

more info:

λ node --version
v12.1.0

λ npm --version
6.9.0

Also asked at stack overflow

@charlie-harvey
Copy link

charlie-harvey commented Jun 3, 2022

Still happening in 2022. I have the exact same error as @opensas

  "dependencies": {
    "winston": "3.7.2",
    "winston-transport": "4.5.0"
  },
  "devDependencies": {
    "typescript": "4.7.2"
  }
λ node --version
v14.19.1

λ npm --version
6.14.6

Fixed

import Transport from 'winston-transport';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants