klaudiosinani / signale

Highly configurable logging utility
MIT License
8.92k stars 232 forks source link

WriteStream object is not accepted in stream array #117

Open tautf opened 3 years ago

tautf commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

private logFile: WriteStream = fs.createWriteStream(process.cwd() + path.join('////', 'logs', 'watchguard.txt'), {
            flags: 'a',
            encoding: 'utf8',
            autoClose: true
        });

is not accepted in SignaleOptions as stream.

To Reproduce Steps to reproduce the behavior.

Create an object of type SignaleObject and at the above as a stream into the array.

Expected behavior A clear and concise description of what you expected to happen. WritableStream is accepted.

Technical Info (please complete the following information)

Additional context Add any other context about the problem here.

private logFile: WriteStream = fs.createWriteStream(process.cwd() + path.join('////', 'logs', 'watchguard.txt'), {
            flags: 'a',
            encoding: 'utf8',
            autoClose: true
        });

private options: SignaleOptions = {
        disabled: false,
        interactive: false,
        logLevel: process.env.LOG_LEVEL,
        scope: '',
        stream: [
            process.stdout,
            this.logFile
        ],
        types: ....

Error: (property) Logger.logFile: fs.WriteStream Type 'WriteStream' is missing the following properties from type 'WriteStream': clearLine, clearScreenDown, cursorTo, moveCursor, and 36 more.ts(2740)

tautf commented 3 years ago

As this Plugin is not under heavy development i tried a little and made the following solution for me, for now:

Overriding the signale.d.ts types file by adding another with the following content:

import { WriteStream } from "fs";
import { SignaleOptions } from "signale";

declare namespace signale {

    interface SignaleOptionsX extends Omit<SignaleOptions, 'stream'> {
        stream?: NodeJS.Process.stdout | WriteStream | WriteStream[];
    }
}

declare const SignaleOptionsX: signale.SignaleOptionsX;

export = signale;

In my class then import { SignaleOptionsX } from '../../types/signale'; and changing SignaleOptions to SignaleOptionsX.

Now

...
stream: [
     process.stdout,
     this.logFile
]
...

Works for me again. Formatting isnt nice, but at least i dont get an error by typescript.