Skip to content

Commit 3634b01

Browse files
authored
🤖 Merge PR DefinitelyTyped#72097 [node] buffer: allow ArrayBuffer | string to be passed to Buffer.from by @sapphi-red
1 parent 24f3fbf commit 3634b01

File tree

9 files changed

+39
-0
lines changed

9 files changed

+39
-0
lines changed

‎types/node/buffer.buffer.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ declare module "buffer" {
160160
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
161161
*/
162162
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer<ArrayBuffer>;
163+
from(arrayOrString: WithImplicitCoercion<ArrayLike<number> | string>): Buffer<ArrayBuffer>;
163164
/**
164165
* Creates a new Buffer using the passed {data}
165166
* @param values to create a new Buffer

‎types/node/test/buffer.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ const result2 = Buffer.concat([utf8Buffer, base64Buffer] as readonly Uint8Array[
149149
Buffer.from({} as { [Symbol.toPrimitive](): number });
150150
}
151151

152+
// ArrayLike or string
153+
{
154+
const arrayOrString = "foo" as number[] | string;
155+
// $ExpectType Buffer || Buffer<ArrayBuffer>
156+
Buffer.from(arrayOrString);
157+
158+
const typedArrayOrString = "foo" as Uint8Array<ArrayBuffer> | string;
159+
// $ExpectType Buffer || Buffer<ArrayBuffer>
160+
Buffer.from(typedArrayOrString);
161+
}
162+
152163
// @ts-expect-error
153164
Buffer.from({});
154165
// @ts-expect-error

‎types/node/ts5.6/buffer.buffer.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ declare module "buffer" {
158158
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
159159
*/
160160
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer;
161+
from(arrayOrString: WithImplicitCoercion<ArrayLike<number> | string>): Buffer;
161162
/**
162163
* Creates a new Buffer using the passed {data}
163164
* @param values to create a new Buffer

‎types/node/v18/buffer.buffer.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ declare module "buffer" {
155155
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
156156
*/
157157
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer<ArrayBuffer>;
158+
from(arrayOrString: WithImplicitCoercion<ArrayLike<number> | string>): Buffer<ArrayBuffer>;
158159
/**
159160
* Creates a new Buffer using the passed {data}
160161
* @param values to create a new Buffer

‎types/node/v18/test/buffer.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ const result2 = Buffer.concat([utf8Buffer, base64Buffer] as readonly Uint8Array[
148148
Buffer.from({} as { [Symbol.toPrimitive](): number });
149149
}
150150

151+
// ArrayLike or string
152+
{
153+
const arrayOrString = "foo" as number[] | string;
154+
// $ExpectType Buffer || Buffer<ArrayBuffer>
155+
Buffer.from(arrayOrString);
156+
157+
const typedArrayOrString = "foo" as Uint8Array<ArrayBuffer> | string;
158+
// $ExpectType Buffer || Buffer<ArrayBuffer>
159+
Buffer.from(typedArrayOrString);
160+
}
161+
151162
// @ts-expect-error
152163
Buffer.from({});
153164
// @ts-expect-error

‎types/node/v18/ts5.6/buffer.buffer.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ declare module "buffer" {
153153
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
154154
*/
155155
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer;
156+
from(arrayOrString: WithImplicitCoercion<ArrayLike<number> | string>): Buffer;
156157
/**
157158
* Creates a new Buffer using the passed {data}
158159
* @param values to create a new Buffer

‎types/node/v20/buffer.buffer.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ declare module "buffer" {
160160
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
161161
*/
162162
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer<ArrayBuffer>;
163+
from(arrayOrString: WithImplicitCoercion<ArrayLike<number> | string>): Buffer<ArrayBuffer>;
163164
/**
164165
* Creates a new Buffer using the passed {data}
165166
* @param values to create a new Buffer

‎types/node/v20/test/buffer.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ const result2 = Buffer.concat([utf8Buffer, base64Buffer] as readonly Uint8Array[
149149
Buffer.from({} as { [Symbol.toPrimitive](): number });
150150
}
151151

152+
// ArrayLike or string
153+
{
154+
const arrayOrString = "foo" as number[] | string;
155+
// $ExpectType Buffer || Buffer<ArrayBuffer>
156+
Buffer.from(arrayOrString);
157+
158+
const typedArrayOrString = "foo" as Uint8Array<ArrayBuffer> | string;
159+
// $ExpectType Buffer || Buffer<ArrayBuffer>
160+
Buffer.from(typedArrayOrString);
161+
}
162+
152163
// @ts-expect-error
153164
Buffer.from({});
154165
// @ts-expect-error

‎types/node/v20/ts5.6/buffer.buffer.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ declare module "buffer" {
158158
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
159159
*/
160160
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer;
161+
from(arrayOrString: WithImplicitCoercion<ArrayLike<number> | string>): Buffer;
161162
/**
162163
* Creates a new Buffer using the passed {data}
163164
* @param values to create a new Buffer

0 commit comments

Comments
 (0)