microsoft / microsoft/TypeScript

Suggestion: use numeric literals for `BYTES_PER_ELEMENT`

Open
#59,557 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: lib.d.ts Experience Enhancement Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

⚙ 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect the typed array declarations in lib.es5.d.ts and lib.es2022.bigint.d.ts, including the static and instance BYTES_PER_ELEMENT properties. Compare the declarations with the issue's proposed literal values and verify that the sample calls accepting 1 | 2 | 4 | 8 type-check correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.