'How to redirect node stdout or stderr to a file in Typescript?

I tried to use a JavaScript solution that looks like below but faced compilation errors.

    const access = fs.createWriteStream('err.log');
    process.stderr.write = access.write.bind(access);

Compilation error:

error TS2322: Type '{ (chunk: any, callback?: ((error: Error | null | undefined) => void) | undefined): boolean; (chunk: any, encoding: BufferEncoding, callback?: ((error: Error | null | undefined) => void) | undefined): boolean; }' is not assignable to type '{ (buffer: string | Uint8Array, cb?: ((err?: Error | undefined) => void) | undefined): boolean; (str: string | Uint8Array, encoding?: BufferEncoding | undefined, cb?: ((err?: Error | undefined) => void) | undefined): boolean; }'.
  Types of parameters 'callback' and 'cb' are incompatible.
    Types of parameters 'err' and 'error' are incompatible.
      Type 'Error | null | undefined' is not assignable to type 'Error | undefined'.
        Type 'null' is not assignable to type 'Error | undefined'.

15     process.stderr.write = access.write.bind(access);

What is the ts way of doing the same operation?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source