Microsoft/TypeScript

Suggestion: use numeric literals for `BYTES_PER_ELEMENT`

Open

#59.557 aperta il 8 ago 2024

Vedi su GitHub
 (0 commenti) (1 reazione) (0 assegnatari)TypeScript (6726 fork)batch import
Domain: lib.d.tsExperience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

⚙ Compilation target

ES5+

⚙ Library

lib.es5.d.ts, lib.es2022.bigint.d.ts

Missing / Incorrect Definition

Suggesting to change the BYTES_PER_ELEMENT static and instance properties as follows:

interface Uint8Array {
    // ...
-   readonly BYTES_PER_ELEMENT: number;
+   readonly BYTES_PER_ELEMENT: 1;
    // ...
}
interface BigInt64ArrayConstructor {
    // ...
-   readonly BYTES_PER_ELEMENT: number;
+   readonly BYTES_PER_ELEMENT: 8;
    // ...
}

Etc.

Sample Code

declare function fn(bytesPerElement: 1 | 2 | 4 | 8): void
// currently reports "Argument of type 'number' is not assignable to parameter of type '1 | 2 | 4 | 8'".
fn(Uint32Array.BYTES_PER_ELEMENT)

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT#examples

Guida contributor