pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.22k stars 874 forks source link

pino({ level, stream }) does not produce type error (typescript stream LoggerOptions | DestinationStream) #1431

Open devinrhode2 opened 2 years ago

devinrhode2 commented 2 years ago

This does not produce a type error:

pino({
  level: 'debug',
  stream: 'asdf' as unknown as pino.DestinationStream,
})

Probably the signature for pino is too complicated. Simplifying it will make writing the TS defs for it simpler (and easier to write+maintain).

I'd suggest something like:

const pino = (destinationStream: DestinationStream, loggerOptions: LoggerOptions) => {}

OR, make it one argument and just put everything into LoggerOptions:

const pino = (loggerOptions: LoggerOptionsWithStream) => {}

A simpler TS type def will probably be more reliable long term and also produce a type error in my situation.

mcollina commented 2 years ago

Why are you are double-casing a wrong parameter?

cc @kibertoad

devinrhode2 commented 2 years ago

I'm not sure what you mean

Double casing a wrong parameter

I assume this is the parameter you are referring to:

const level = 'debug'
const stream = someWriteSteam
pino(
  // This param?
  { level, steam }
)

I don't know what you mean by "double casing"

I believe I originally encountered this bug by updating from pino.. 7.0.2? All the way to latest 7.11

devinrhode2 commented 2 years ago

It seems some older version of pino supported loggerOptions.stream

mcollina commented 2 years ago

Do you know which version supported it? I'm mot convinced we ever did.

Anyway, would you like to send a Pull Request to address this issue? Remember to add unit tests.

devinrhode2 commented 2 years ago

Of the two ideas I Suggested both are pretty hard breaking changes, likely calling for a major version bump. I don't think it'd be very hard to make the change, the main dilemma would be the version bump.

Given you think stream was never a supported property, I have few questions -what if it was added? Something like "writeStream"? "pino" would then only accept one options object parameter

Which, brings into question, why was it setup as a separate parameter in the first place? Surely there will be something we can find via git blame

mcollina commented 2 years ago

This is implementable without having any breaking change.

devinrhode2 commented 2 years ago

I don't know exactly why TS isn't creating a type error in my original example. I'm guessing because LoggerOptions type is too loose.. so if it's possible to more directly address that issue, that's the change we should do.

But in future major version, I think we should consider making stream a property