Jeff Pal
Jeff Pal

Reputation: 1674

what is the proper type for process.stdin in typescript/nodejs?

I have a variable that can be fs.createReadStream('file-path') or process.stdin:

const input: any = filePath ? fs.createReadStream(filePath) : process.stdin;

but I'm not sure about which type I must use. So I wonder if both returning are either of type net.Socket, fs.ReadStream or stream.Redable.

What is the suitable type to replace any? Would the declaration below be correct?

let input: null | NodeJS.ReadStream = null;
input: any = filePath ? fs.createReadStream(filePath) : process.stdin;

Upvotes: 0

Views: 730

Answers (1)

ps2goat
ps2goat

Reputation: 8475

This isn't a full answer as to what you should use, but how you can find it.

I specify types like you do, but if you really want to know what the type[s] could be, simply don't specify one. VS Code will then tell you the proper type when you hover over the variable name. You can even copy it from the intellisense window and paste it into the editor.

screenshot of vs code's intellisense for type inference

    const filePath: fs.PathLike = '';    
    const input = filePath ? fs.createReadStream(filePath) : process.stdin;

So based on this, the proper type is:

    const filePath: fs.PathLike = '';    
    const input: fs.ReadStream | (NodeJS.ReadStream & { fd: 0; }) = 
        filePath ? fs.createReadStream(filePath) : process.stdin;

Of course, this is for my current setup and yours may vary by version.

Upvotes: 1

Related Questions