Skip to content
Advertisement

stream.write This expression is not callable

I am trying to a write a function that takes either writeable stream (createWriteStream) or process.stdout/.stderr but typescript keeps throwing this error. Error goes away when I do conditional type check.

JavaScript

Error message at line 3

JavaScript

Advertisement

Answer

Both NodeJS.WriteStream and WriteStream overload the write() method, but they use different signatures, resulting in the error you are seeing.

Instead of defining the union type between these two types, you can define the type of the stream parameter using Writable, that is extended by both:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement