Microsoft/TypeScript#23155 - Type error in Buffer.from() (#24966)

This commit is contained in:
Alexander T
2018-04-13 08:50:15 -07:00
committed by Mohamed Hegazy
parent 3b587e9ce3
commit dcf3384ac2
10 changed files with 86 additions and 85 deletions
+4 -6
View File
@@ -22,6 +22,7 @@
// Nicolas Even <https://github.com/n-e>
// Mohsen Azimi <https://github.com/mohsen1>
// Hoàng Văn Khải <https://github.com/KSXGitHub>
// Alexander T. <https://github.com/a-tarasyuk>
// 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.
+13 -10
View File
@@ -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);
+3 -6
View File
@@ -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.
+13 -10
View File
@@ -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);
+3 -6
View File
@@ -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.
+13 -10
View File
@@ -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);
+3 -6
View File
@@ -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.
+18 -15
View File
@@ -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;
+3 -6
View File
@@ -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.
+13 -10
View File
@@ -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);