From d60528f838fa465ecc3ffbb82f847913aa5a4bb3 Mon Sep 17 00:00:00 2001 From: Matthias Kunnen Date: Wed, 26 Jun 2019 18:48:55 +0200 Subject: [PATCH] [serialport] Add error nullability (#36451) * [serialport] Fix error in callback can be null Most methods are inherited from Node stream and according to types that means that the err parameter can be null or undefined. See https://serialport.io/docs/guide-usage and look for function(err). See https://github.com/serialport/node-serialport/blob/ef80d830804b0bf22d37c13d19eaa674800f9691/packages/serialport/test/integration.js#L55 * [serialport] Add correct type to test_list_ports_promise * [serialport] Enabled strict-type-predicates for testing --- types/serialport/index.d.ts | 12 ++++++------ types/serialport/serialport-tests.ts | 26 ++++++++++++++++---------- types/serialport/tslint.json | 7 ++++++- 3 files changed, 28 insertions(+), 17 deletions(-) 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 + } +}