Skip to content

Commit 0448d00

Browse files
committed
Add README
1 parent ee1061b commit 0448d00

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# eslint-plugin-enforce-uint8array-arraybuffer
2+
3+
This ESLint rule enforces that any `Uint8Array` type declaration explicitly includes `<ArrayBuffer>` as its generic parameter.
4+
An auto-fix feature is also implemented.
5+
6+
Using `<ArrayBuffer>` ensures compatibility with WebCrypto APIs, `Blob`s, and other browser features, following a TS [change](https://github.com/microsoft/TypeScript/pull/59417) in v5.9 the default `ArrayBufferLike` parameter is no longer guaranteed to be compatible with `ArrayBuffer` (due to differences with `SharedArrayBuffer`) .
7+
8+
## Installation
9+
10+
```sh
11+
npm i --save-dev @protontech/eslint-plugin-enforce-uint8array-arraybuffer
12+
```
13+
14+
## Usage
15+
16+
Add the plugin and rule to your ESLint config:
17+
18+
```jsonc
19+
{
20+
"plugins": ["enforce-uint8array-arraybuffer"],
21+
"rules": {
22+
"enforce-uint8array-arraybuffer/enforce-uint8array-arraybuffer": "error"
23+
}
24+
}
25+
```
26+
27+
## Example behavior
28+
29+
These usages are correct:
30+
31+
```ts
32+
const a = new Uint8Array(); // this is automatically instantiated as Uint8Array<ArrayBuffer>
33+
function f(data: Uint8Array<ArrayBuffer>) {}
34+
type T = Promise<Uint8Array<ArrayBuffer>[]>;
35+
```
36+
37+
While these will trigger eslint errors (`Uint8Array must be used as Uint8Array<ArrayBuffer>`), but can be auto-fixed.
38+
39+
```ts
40+
function f(data: Uint8Array) {} // missingGeneric error
41+
type T = Promise<Uint8Array[]>; // missingGeneric error
42+
```
43+
44+
If a generic argument is specified other than `ArrayBuffer`, the linter will also error (`Uint8Array generic argument must be exactly 'ArrayBuffer'`), but it will require manual resolution:
45+
46+
```ts
47+
function f(data: Uint8Array<ArrayBufferLike>) {} // wrongGeneric error
48+
```

0 commit comments

Comments
 (0)