node-ssh icon indicating copy to clipboard operation
node-ssh copied to clipboard

@types/ssh2 are required when using TS `strictMode`

Open matbour opened this issue 4 years ago • 4 comments

When using Typescript's strictMode, the compilation fails with the following error:

node_modules/node-ssh/lib/typings/index.d.ts:2:110 - error TS7016: Could not find a declaration file for module 'ssh2'. './node_modules/ssh2/lib/client.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/ssh2` if it exists or add a new declaration (.d.ts) file containing `declare module 'ssh2';`

2 import SSH2, { ConnectConfig, ClientChannel, SFTPWrapper, ExecOptions, PseudoTtyOptions, ShellOptions } from 'ssh2';
                                                                                                               ~~~~~~

node_modules/node-ssh/lib/typings/index.d.ts:3:41 - error TS7016: Could not find a declaration file for module 'ssh2-streams'. './node_modules/ssh2-streams/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/ssh2-streams` if it exists or add a new declaration (.d.ts) file containing `declare module 'ssh2-streams';`

3 import { Prompt, TransferOptions } from 'ssh2-streams';
                                          ~~~~~~~~~~~~~~


Found 2 errors.

The fix is quite easy, simply install @types/ssh2 (which also installs @types/ssh2-streams, see here:

npm i --save-dev @types/ssh2

I think that installing the types dependencies is not the responsibility of the end user. Shouldn't the @types/ssh2 be installed as part of node-ssh dependencies or peerDependencies?

matbour avatar May 04 '21 10:05 matbour

I am hesitant to add @types/ssh2 to dependencies, mainly because it'll be installed for people who are not Typescript users

steelbrain avatar May 04 '21 18:05 steelbrain

Indeed, but on the other hand, node-ssh is distributing its own typings inside the package, which are not used by non-TS users either. We could also imagine having some kind of @types/node-ssh which would require the @types/ssh2 as dependencies.

At least, I think that note in the README can be relevant if you choose to not link the @types/ssh2.

matbour avatar May 04 '21 23:05 matbour

@types/ssh2 no longer depends on @types/ssh2-streams anymore either, so both are required for a build without skipLibCheck to pass.

ramblingenzyme avatar Aug 18 '22 00:08 ramblingenzyme

The other issue is that the types from ssh2-streams are part of the public interface of the library, so if it's up to the library consumer to install @types/ssh2-streams, then when they upgrade those type definitions it's possible for the types of node-ssh to change in a way that breaks at runtime.

ramblingenzyme avatar Aug 18 '22 00:08 ramblingenzyme