diff --git a/types/serialport/index.d.ts b/types/serialport/index.d.ts index 0307b323d5..41dba7d66c 100644 --- a/types/serialport/index.d.ts +++ b/types/serialport/index.d.ts @@ -21,12 +21,12 @@ declare class SerialPort extends Stream.Duplex { open(callback?: SerialPort.ErrorCallback): void; update(options: SerialPort.UpdateOptions, callback?: SerialPort.ErrorCallback): void; - write(data: string| number[] | Buffer, callback?: (error: any, bytesWritten: number) => void): boolean; - write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: any, bytesWritten: number) => void): boolean; + write(data: string| number[] | Buffer, callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean; + write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean; read(size?: number): string | Buffer | null; - close(callback?: (error: Error) => void): void; + close(callback?: (error?: Error | null) => void): void; set(options: SerialPort.SetOptions, callback?: SerialPort.ErrorCallback): void; get(callback?: SerialPort.ModemBitsCallback): void; @@ -46,9 +46,9 @@ declare class SerialPort extends Stream.Duplex { declare namespace SerialPort { // Callbacks Type Defs - type ErrorCallback = (error: Error) => void; - type ModemBitsCallback = (error: Error, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void; - type ListCallback = (error: Error, port: any[]) => void; + type ErrorCallback = (error?: Error | null) => void; + type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void; + type ListCallback = (error: Error | null | undefined, ports: any[]) => void; // Options Type Defs interface OpenOptions { diff --git a/types/serialport/serialport-tests.ts b/types/serialport/serialport-tests.ts index 476199b506..9ab7ac45a9 100644 --- a/types/serialport/serialport-tests.ts +++ b/types/serialport/serialport-tests.ts @@ -8,7 +8,7 @@ function test_basic_connect() { function test_connect_config() { const port1 = new SerialPort('', { - }, (error: Error) => {}); + }, error => {}); const port4 = new SerialPort('', { autoOpen: false, @@ -25,7 +25,13 @@ function test_connect_config() { vmin: 1, vtime: 1 } - }, (error: Error) => {}); + }, + error => { + if (error !== null) { + console.error(error); + } + } + ); } function test_open() { @@ -41,8 +47,8 @@ function test_update() { function test_write() { const port = new SerialPort(''); - port.write('test', (error: Error) => {}); - port.write('test', 'utf8', (error: Error) => {}); + port.write('test', (error?: Error | null) => {}); + port.write('test', 'utf8', (error?: Error | null) => {}); } function test_read() { @@ -54,13 +60,13 @@ function test_read() { function test_close() { const port = new SerialPort(''); - port.close((error: Error) => {}); + port.close((error?: Error | null) => {}); } function test_set() { const port = new SerialPort(''); - port.set({}, (error: Error) => {}); + port.set({}, (error?: Error | null) => {}); } function test_get() { @@ -72,13 +78,13 @@ function test_get() { function test_flush() { const port = new SerialPort(''); - port.flush((error: Error) => {}); + port.flush((error?: Error | null) => {}); } function test_drain() { const port = new SerialPort(''); - port.drain((error: Error) => {}); + port.drain((error?: Error | null) => {}); } function test_pause_resume() { @@ -130,10 +136,10 @@ function test_properties() { function test_list_ports_promise() { const ports = SerialPort .list() - .then((ports: any) => {}) + .then((ports: SerialPort.PortInfo[]) => {}) .catch((err: Error) => {}); } function test_list_ports_callback() { - const ports = SerialPort.list((error: Error, port: any[]) => {}); + const ports = SerialPort.list((error: Error | null | undefined, ports: any[]) => {}); } diff --git a/types/serialport/tslint.json b/types/serialport/tslint.json index 2750cc0197..fcae8d5d63 100644 --- a/types/serialport/tslint.json +++ b/types/serialport/tslint.json @@ -1 +1,6 @@ -{ "extends": "dtslint/dt.json" } \ No newline at end of file +{ + "extends": "dtslint/dt.json", + "rules": { + "strict-type-predicates": true + } +}