Skip to content

Commit c663e84

Browse files
authored
fix: type issue after ts5.7 (#14)
1 parent 116f64f commit c663e84

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

src/base32.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//inspired by oslo implementation by pilcrowonpaper: https://github.com/pilcrowonpaper/oslo/blob/main/src/encoding/base32.ts
22

3-
import type { TypedArray } from "./type";
3+
import type { TypedArray, Uint8Array_ } from "./type";
44

55
/**
66
* Returns the Base32 alphabet based on the encoding type.
@@ -34,7 +34,7 @@ function createDecodeMap(alphabet: string): Map<string, number> {
3434
* @returns The Base32 encoded string.
3535
*/
3636
function base32Encode(
37-
data: Uint8Array,
37+
data: Uint8Array_,
3838
alphabet: string,
3939
padding: boolean,
4040
): string {
@@ -69,7 +69,7 @@ function base32Encode(
6969
* @param alphabet - The Base32 alphabet to use.
7070
* @returns The decoded Uint8Array.
7171
*/
72-
function base32Decode(data: string, alphabet: string): Uint8Array {
72+
function base32Decode(data: string, alphabet: string): Uint8Array_ {
7373
const decodeMap = createDecodeMap(alphabet);
7474
const result: number[] = [];
7575
let buffer = 0;
@@ -120,7 +120,7 @@ export const base32 = {
120120
* @param data - The Base32 encoded string or ArrayBuffer/TypedArray.
121121
* @returns The decoded Uint8Array.
122122
*/
123-
decode(data: string | ArrayBuffer | TypedArray): Uint8Array {
123+
decode(data: string | ArrayBuffer | TypedArray): Uint8Array_ {
124124
if (typeof data !== "string") {
125125
data = new TextDecoder().decode(data);
126126
}
@@ -156,7 +156,7 @@ export const base32hex = {
156156
* @param data - The Base32hex encoded string.
157157
* @returns The decoded Uint8Array.
158158
*/
159-
decode(data: string): Uint8Array {
159+
decode(data: string): Uint8Array_ {
160160
const alphabet = getAlphabet(true);
161161
return base32Decode(data, alphabet);
162162
},

src/base64.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//inspired by oslo implementation by pilcrowonpaper: https://github.com/pilcrowonpaper/oslo/blob/main/src/encoding/base64.ts
22

3-
import type { TypedArray } from "./type";
3+
import type { TypedArray, Uint8Array_ } from "./type";
44

55
function getAlphabet(urlSafe: boolean): string {
66
return urlSafe
@@ -9,7 +9,7 @@ function getAlphabet(urlSafe: boolean): string {
99
}
1010

1111
function base64Encode(
12-
data: Uint8Array,
12+
data: Uint8Array_,
1313
alphabet: string,
1414
padding: boolean,
1515
): string {
@@ -38,7 +38,7 @@ function base64Encode(
3838
return result;
3939
}
4040

41-
function base64Decode(data: string, alphabet: string): Uint8Array {
41+
function base64Decode(data: string, alphabet: string): Uint8Array_ {
4242
const decodeMap = new Map<string, number>();
4343
for (let i = 0; i < alphabet.length; i++) {
4444
decodeMap.set(alphabet[i]!, i);

src/type.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ export type TypedArray =
1010
| BigInt64Array
1111
| BigUint64Array;
1212

13+
/**
14+
* Equivalent to `Uint8Array` before TypeScript 5.7, and `Uint8Array<ArrayBuffer>` in TypeScript 5.7
15+
* and beyond.
16+
*
17+
* **Context**
18+
*
19+
* `Uint8Array` became a generic type in TypeScript 5.7, requiring types defined simply as
20+
* `Uint8Array` to be refactored to `Uint8Array<ArrayBuffer>` starting in Deno 2.2. `Uint8Array` is
21+
* _not_ generic in Deno 2.1.x and earlier, though, so this type helps bridge this gap.
22+
*
23+
* Inspired by Deno's std library:
24+
*
25+
* https://github.com/denoland/std/blob/b5a5fe4f96b91c1fe8dba5cc0270092dd11d3287/bytes/_types.ts#L11
26+
*/
27+
export type Uint8Array_ = ReturnType<Uint8Array["slice"]>;
28+
1329
export type SHAFamily = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
1430
export type EncodingFormat =
1531
| "hex"

0 commit comments

Comments
 (0)