Skip to content

Suggestion: use numeric literals for BYTES_PER_ELEMENT #59557

@lionel-rowe

Description

@lionel-rowe

⚙ 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptExperience EnhancementNoncontroversial enhancementsHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions