Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
472 stars 177 forks source link

Build using Typescript error: Cannot use namespace 'SerialPort' as a type. #342

Open Zandor300 opened 5 months ago

Zandor300 commented 5 months ago

Receiving the following error if I want to build using Typescript. Before I was just running the plain Javascript.

jsmodbus:           4.0.6 (Since that is the last one published on npm)
serialport:        12.0.0
@types/serialport:  8.0.5
typescript:         5.5.3
$ tsc
node_modules/jsmodbus/dist/modbus-rtu-client.d.ts:6:55 - error TS2709: Cannot use namespace 'SerialPort' as a type.

6 export default class ModbusRTUClient extends MBClient<SerialPort, ModbusRTURequest> {
                                                        ~~~~~~~~~~

node_modules/jsmodbus/dist/modbus-rtu-client.d.ts:9:25 - error TS2709: Cannot use namespace 'SerialPort' as a type.

9     constructor(socket: SerialPort, address: number, timeout?: number);
                          ~~~~~~~~~~

node_modules/jsmodbus/dist/modbus-rtu-server.d.ts:6:25 - error TS2709: Cannot use namespace 'SerialPort' as a type.

6     constructor(socket: SerialPort, options?: Partial<IModbusServerOptions>);
                          ~~~~~~~~~~

node_modules/jsmodbus/dist/rtu-client-request-handler.d.ts:7:83 - error TS2709: Cannot use namespace 'SerialSocket' as a type.

7 export default class ModbusRTUClientRequestHandler extends MBClientRequestHandler<SerialSocket, ModbusRTURequest> {
                                                                                    ~~~~~~~~~~~~

node_modules/jsmodbus/dist/rtu-client-request-handler.d.ts:11:25 - error TS2709: Cannot use namespace 'SerialSocket' as a type.

11     constructor(socket: SerialSocket, address: number, timeout?: number);
                           ~~~~~~~~~~~~

Found 5 errors in 3 files.
stefanpoeter commented 5 months ago

The module is build against serialport version "serialport": "^7.1.5" see package.json.

Zandor300 commented 4 months ago

@stefanpoeter I use it with serialport v12 and it works fine, only the exact types used are incorrect when building using Typescript.

stefanpoeter commented 4 months ago

That sounds like incompatibilties. If you want to port I am happy for any PR. As said it is compatible to 7.1.5.

swisspol commented 3 months ago

This seems to work around the compiling issue in TypeScript:

npm install serialport@9
npm i --save-dev @types/serialport
stefanpoeter commented 2 months ago

Have you checked adding this to the package.json?