2
votes

I'm trying to manually create types for winston-syslog (direct link to main file) in node, but I'm having issues. When I create the following definitions:

// File: src/types/winston-syslog/index.d.ts
declare module 'winston-syslog' {
    import * as Transport from 'winston-transport';

    export interface SyslogTransportOptions extends Transport.TransportStreamOptions {
        host?: string;
        port?: number;
        path?: string;
        protocol?: string;
        pid?: number;
        facility?: string;
        localhost?: string;
        type?: string;
        app_name?: string;
        eol?: string;
        levels?: {[key: string]: number};
    }

    export interface Syslog extends Transport {
        new(options?: SyslogTransportOptions): Syslog;
    }

}

I can't create instances of Syslog, as Typescript fails with the error:

error TS2693: 'Syslog' only refers to a type, but is being used as a value here.

This is my (heavily reduced) main file:

// File: src/Log.ts
import {Syslog, SyslogTransportOptions} from 'winston-syslog';
const transportOptions = {/* some values here */};
const syslogTransport = new Syslog(transportOptions);

I'm guessing there's something wrong with how I defined the Syslog class, but this is exactly how it is done in winston internally.

1

1 Answers

3
votes

The Syslog is an instance in your 'winston-syslog' package, not an interface. You can write your type definitions slightly different in a separate *.d.ts file.

declare module "winston-syslog" {
  export interface ISyslog {
    new (options: any): ISyslog;
    log(message: string): void;
  }

  export const Syslog: ISyslog;
}

Then you can use the typings like so:

import * as Test from "winston-syslog";

const t = new Test.Syslog({}); // The type is ISyslog

t.log("blabla");