From dcf3384ac2714c299a676cdc6dacabf1a47657c2 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Fri, 13 Apr 2018 18:50:15 +0300 Subject: [PATCH] Microsoft/TypeScript#23155 - Type error in Buffer.from() (#24966) --- types/node/index.d.ts | 10 ++++------ types/node/node-tests.ts | 23 +++++++++++++---------- types/node/v4/index.d.ts | 9 +++------ types/node/v4/node-tests.ts | 23 +++++++++++++---------- types/node/v6/index.d.ts | 9 +++------ types/node/v6/node-tests.ts | 23 +++++++++++++---------- types/node/v7/index.d.ts | 9 +++------ types/node/v7/node-tests.ts | 33 ++++++++++++++++++--------------- types/node/v8/index.d.ts | 9 +++------ types/node/v8/node-tests.ts | 23 +++++++++++++---------- 10 files changed, 86 insertions(+), 85 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index ad70b302f9..d27e680aed 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -22,6 +22,7 @@ // Nicolas Even // Mohsen Azimi // Hoàng Văn Khải +// Alexander T. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /** inspector module types */ @@ -218,10 +219,6 @@ declare var Buffer: { */ new(buffer: Buffer): Buffer; prototype: Buffer; - /** - * Allocates a new Buffer using an {array} of octets. - */ - from(array: any[]): Buffer; /** * When passed a reference to the .buffer property of a TypedArray instance, * the newly created Buffer will share the same allocated memory as the TypedArray. @@ -232,9 +229,10 @@ declare var Buffer: { */ from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer; /** - * Copies the passed {buffer} data onto a new Buffer instance. + * Creates a new Buffer using the passed {data} + * @param data data to create a new Buffer */ - from(buffer: Buffer): Buffer; + from(data: any[] | string | Buffer | ArrayBuffer /*| TypedArray*/): Buffer; /** * Creates a new Buffer containing the given JavaScript string {str}. * If provided, the {encoding} parameter identifies the character encoding. diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 72d0e6e0eb..776364a740 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -423,9 +423,19 @@ function bufferTests() { buf.swap64(); } - // Class Method: Buffer.from(array) + // Class Method: Buffer.from(data) { - const buf: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Array + const buf1: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Buffer + const buf2: Buffer = Buffer.from(buf1); + // String + const buf3: Buffer = Buffer.from('this is a tést'); + // ArrayBuffer + const arr: Uint16Array = new Uint16Array(2); + arr[0] = 5000; + arr[1] = 4000; + const buf4: Buffer = Buffer.from(arr.buffer); } // Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]]) @@ -435,22 +445,15 @@ function bufferTests() { arr[1] = 4000; let buf: Buffer; - buf = Buffer.from(arr.buffer); buf = Buffer.from(arr.buffer, 1); buf = Buffer.from(arr.buffer, 0, 1); } - // Class Method: Buffer.from(buffer) - { - const buf1: Buffer = Buffer.from('buffer'); - const buf2: Buffer = Buffer.from(buf1); - } - // Class Method: Buffer.from(str[, encoding]) { - const buf1: Buffer = Buffer.from('this is a tést'); const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex'); } + // Class Method: Buffer.alloc(size[, fill[, encoding]]) { const buf1: Buffer = Buffer.alloc(5); diff --git a/types/node/v4/index.d.ts b/types/node/v4/index.d.ts index ada62b683e..4793b4e9f7 100644 --- a/types/node/v4/index.d.ts +++ b/types/node/v4/index.d.ts @@ -164,10 +164,6 @@ declare var Buffer: { */ new (buffer: Buffer): Buffer; prototype: Buffer; - /** - * Allocates a new Buffer using an {array} of octets. - */ - from(array: any[]): Buffer; /** * When passed a reference to the .buffer property of a TypedArray instance, * the newly created Buffer will share the same allocated memory as the TypedArray. @@ -178,9 +174,10 @@ declare var Buffer: { */ from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?:number): Buffer; /** - * Copies the passed {buffer} data onto a new Buffer instance. + * Creates a new Buffer using the passed {data} + * @param data data to create a new Buffer */ - from(buffer: Buffer): Buffer; + from(data: any[] | string | Buffer | ArrayBuffer /*| TypedArray*/): Buffer; /** * Creates a new Buffer containing the given JavaScript string {str}. * If provided, the {encoding} parameter identifies the character encoding. diff --git a/types/node/v4/node-tests.ts b/types/node/v4/node-tests.ts index 2b3be03f8d..6409e83e18 100644 --- a/types/node/v4/node-tests.ts +++ b/types/node/v4/node-tests.ts @@ -240,9 +240,19 @@ function bufferTests() { var result1 = Buffer.concat([utf8Buffer, base64Buffer]); var result2 = Buffer.concat([utf8Buffer, base64Buffer], 9999999); - // Class Method: Buffer.from(array) + // Class Method: Buffer.from(data) { - const buf: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Array + const buf1: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Buffer + const buf2: Buffer = Buffer.from(buf1); + // String + const buf3: Buffer = Buffer.from('this is a tést'); + // ArrayBuffer + const arr: Uint16Array = new Uint16Array(2); + arr[0] = 5000; + arr[1] = 4000; + const buf4: Buffer = Buffer.from(arr.buffer); } // Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]]) @@ -252,22 +262,15 @@ function bufferTests() { arr[1] = 4000; let buf: Buffer; - buf = Buffer.from(arr.buffer); buf = Buffer.from(arr.buffer, 1); buf = Buffer.from(arr.buffer, 0, 1); } - // Class Method: Buffer.from(buffer) - { - const buf1: Buffer = Buffer.from('buffer'); - const buf2: Buffer = Buffer.from(buf1); - } - // Class Method: Buffer.from(str[, encoding]) { - const buf1: Buffer = Buffer.from('this is a tést'); const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex'); } + // Class Method: Buffer.alloc(size[, fill[, encoding]]) { const buf1: Buffer = Buffer.alloc(5); diff --git a/types/node/v6/index.d.ts b/types/node/v6/index.d.ts index ed9949a9ac..8cde8ee611 100644 --- a/types/node/v6/index.d.ts +++ b/types/node/v6/index.d.ts @@ -164,10 +164,6 @@ declare var Buffer: { */ new (buffer: Buffer): Buffer; prototype: Buffer; - /** - * Allocates a new Buffer using an {array} of octets. - */ - from(array: any[]): Buffer; /** * When passed a reference to the .buffer property of a TypedArray instance, * the newly created Buffer will share the same allocated memory as the TypedArray. @@ -178,9 +174,10 @@ declare var Buffer: { */ from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer; /** - * Copies the passed {buffer} data onto a new Buffer instance. + * Creates a new Buffer using the passed {data} + * @param data data to create a new Buffer */ - from(buffer: Buffer): Buffer; + from(data: any[] | string | Buffer | ArrayBuffer /*| TypedArray*/): Buffer; /** * Creates a new Buffer containing the given JavaScript string {str}. * If provided, the {encoding} parameter identifies the character encoding. diff --git a/types/node/v6/node-tests.ts b/types/node/v6/node-tests.ts index 6cfb45692b..f9d5a9cd62 100644 --- a/types/node/v6/node-tests.ts +++ b/types/node/v6/node-tests.ts @@ -321,9 +321,19 @@ function bufferTests() { buf.swap64(); } - // Class Method: Buffer.from(array) + // Class Method: Buffer.from(data) { - const buf: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Array + const buf1: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Buffer + const buf2: Buffer = Buffer.from(buf1); + // String + const buf3: Buffer = Buffer.from('this is a tést'); + // ArrayBuffer + const arr: Uint16Array = new Uint16Array(2); + arr[0] = 5000; + arr[1] = 4000; + const buf4: Buffer = Buffer.from(arr.buffer); } // Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]]) @@ -333,22 +343,15 @@ function bufferTests() { arr[1] = 4000; let buf: Buffer; - buf = Buffer.from(arr.buffer); buf = Buffer.from(arr.buffer, 1); buf = Buffer.from(arr.buffer, 0, 1); } - // Class Method: Buffer.from(buffer) - { - const buf1: Buffer = Buffer.from('buffer'); - const buf2: Buffer = Buffer.from(buf1); - } - // Class Method: Buffer.from(str[, encoding]) { - const buf1: Buffer = Buffer.from('this is a tést'); const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex'); } + // Class Method: Buffer.alloc(size[, fill[, encoding]]) { const buf1: Buffer = Buffer.alloc(5); diff --git a/types/node/v7/index.d.ts b/types/node/v7/index.d.ts index 950e143536..e8290f204c 100644 --- a/types/node/v7/index.d.ts +++ b/types/node/v7/index.d.ts @@ -175,10 +175,6 @@ declare var Buffer: { */ new (buffer: Buffer): Buffer; prototype: Buffer; - /** - * Allocates a new Buffer using an {array} of octets. - */ - from(array: any[]): Buffer; /** * When passed a reference to the .buffer property of a TypedArray instance, * the newly created Buffer will share the same allocated memory as the TypedArray. @@ -189,9 +185,10 @@ declare var Buffer: { */ from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer; /** - * Copies the passed {buffer} data onto a new Buffer instance. + * Creates a new Buffer using the passed {data} + * @param data data to create a new Buffer */ - from(buffer: Buffer): Buffer; + from(data: any[] | string | Buffer | ArrayBuffer /*| TypedArray*/): Buffer; /** * Creates a new Buffer containing the given JavaScript string {str}. * If provided, the {encoding} parameter identifies the character encoding. diff --git a/types/node/v7/node-tests.ts b/types/node/v7/node-tests.ts index cc46b1f8e4..0ea05066b4 100644 --- a/types/node/v7/node-tests.ts +++ b/types/node/v7/node-tests.ts @@ -303,9 +303,19 @@ function bufferTests() { buf.swap64(); } - // Class Method: Buffer.from(array) + // Class Method: Buffer.from(data) { - const buf: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Array + const buf1: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Buffer + const buf2: Buffer = Buffer.from(buf1); + // String + const buf3: Buffer = Buffer.from('this is a tést'); + // ArrayBuffer + const arr: Uint16Array = new Uint16Array(2); + arr[0] = 5000; + arr[1] = 4000; + const buf4: Buffer = Buffer.from(arr.buffer); } // Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]]) @@ -315,10 +325,15 @@ function bufferTests() { arr[1] = 4000; let buf: Buffer; - buf = Buffer.from(arr.buffer); buf = Buffer.from(arr.buffer, 1); buf = Buffer.from(arr.buffer, 0, 1); } + + // Class Method: Buffer.from(str[, encoding]) + { + const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex'); + } + // Class Method: Buffer.alloc(size[, fill[, encoding]]) { const buf1: Buffer = Buffer.alloc(5); @@ -334,18 +349,6 @@ function bufferTests() { const buf: Buffer = Buffer.allocUnsafeSlow(10); } - // Class Method: Buffer.from(buffer) - { - const buf1: Buffer = Buffer.from('buffer'); - const buf2: Buffer = Buffer.from(buf1); - } - - // Class Method: Buffer.from(str[, encoding]) - { - const buf1: Buffer = Buffer.from('this is a tést'); - const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex'); - } - // Test that TS 1.6 works with the 'as Buffer' annotation // on isBuffer. var a: Buffer | number; diff --git a/types/node/v8/index.d.ts b/types/node/v8/index.d.ts index b11601895e..642a957eca 100644 --- a/types/node/v8/index.d.ts +++ b/types/node/v8/index.d.ts @@ -217,10 +217,6 @@ declare var Buffer: { */ new(buffer: Buffer): Buffer; prototype: Buffer; - /** - * Allocates a new Buffer using an {array} of octets. - */ - from(array: any[]): Buffer; /** * When passed a reference to the .buffer property of a TypedArray instance, * the newly created Buffer will share the same allocated memory as the TypedArray. @@ -231,9 +227,10 @@ declare var Buffer: { */ from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer; /** - * Copies the passed {buffer} data onto a new Buffer instance. + * Creates a new Buffer using the passed {data} + * @param data data to create a new Buffer */ - from(buffer: Buffer): Buffer; + from(data: any[] | string | Buffer | ArrayBuffer /*| TypedArray*/): Buffer; /** * Creates a new Buffer containing the given JavaScript string {str}. * If provided, the {encoding} parameter identifies the character encoding. diff --git a/types/node/v8/node-tests.ts b/types/node/v8/node-tests.ts index 0013486263..2546184d0a 100644 --- a/types/node/v8/node-tests.ts +++ b/types/node/v8/node-tests.ts @@ -397,9 +397,19 @@ function bufferTests() { buf.swap64(); } - // Class Method: Buffer.from(array) + // Class Method: Buffer.from(data) { - const buf: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Array + const buf1: Buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); + // Buffer + const buf2: Buffer = Buffer.from(buf1); + // String + const buf3: Buffer = Buffer.from('this is a tést'); + // ArrayBuffer + const arr: Uint16Array = new Uint16Array(2); + arr[0] = 5000; + arr[1] = 4000; + const buf4: Buffer = Buffer.from(arr.buffer); } // Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]]) @@ -409,22 +419,15 @@ function bufferTests() { arr[1] = 4000; let buf: Buffer; - buf = Buffer.from(arr.buffer); buf = Buffer.from(arr.buffer, 1); buf = Buffer.from(arr.buffer, 0, 1); } - // Class Method: Buffer.from(buffer) - { - const buf1: Buffer = Buffer.from('buffer'); - const buf2: Buffer = Buffer.from(buf1); - } - // Class Method: Buffer.from(str[, encoding]) { - const buf1: Buffer = Buffer.from('this is a tést'); const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex'); } + // Class Method: Buffer.alloc(size[, fill[, encoding]]) { const buf1: Buffer = Buffer.alloc(5);