microsoft / microsoft/TypeScript

TypedArray constructor compile error

Open
#61,134 4 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Feb 7, 2025.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 Search Terms

When creating a class for a typed array, if this class is returned by indexing into an object, I get a compile error.

See attached playground link.

🕗 Version & Regression Information
  • This changed between versions 5.7.3 and 5.4.2, it used to work in 5.4.2
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBDAnmYcAqzgBMCCUoCGiAwhAHYDOMUArgMYzRwC8AUAJAA+cAqgJZkYADjyES5KrQbQO3foICMANlFFSlavUZRZvATADMAJlXiNU7boCSgkfjUTN0nVzg2Yy0+slaZr98ZejhZ+3ABiADYQBIYm9mY+zqysoJCwcHSOcN4wBIIULHAA3qxwcABCAJpoAKIAXHAArApGAAwANKW8AHIAylYA4t01ACIA+lW1Dc1GCp1lvQASAPIASmjTLUbzPf1Do2NLaxtNLQY7Vt0nMwAsnew8fYPD45fXLY2dHGEAMss47yMSk6AF9kmVUtB4JkNHACPFyogMChCiV2ABtHJ5GAUAB0kxqAF0yiTSWUGu47GJ7pjHNi8Y89i8JtUieS9LZTDSsflcUd1sSyaSKYoVPFuXTeYzngd+WhiQ15B4xdSOLSNPTcW9BULhW5BIFxWqeTjcdL9q8rjrFfpDaqMSa8b9-vLdSSGpForEuawQXCCjCqABuZKsQPwABGNAAZtHgFBCmRgAB3OCmcoxuNQAAUAEoQ6wAPSFuDLADWrBKZXDafixAiBAoFAayOwQXMvgTzH1wlMIerWToiZTtbE9cbFGzUdj8facA6cAU+d9yWL2QgAFswHwIqgavgZFWMoPN5Ak4JW4VHfjWf3j7CvA2my3MLg68FO4V4WJEa30Zkt3IYAL0wQk7xrIduyTVNHwnKdM1nec5yXIM4DXGpwGABhsHnABaBQ4SgABzGgN2AnEV2SIA

💻 Code
export type TypedArrayConstructor =
	| Uint8ArrayConstructor
	| Uint16ArrayConstructor
	| Uint32ArrayConstructor
	| Int8ArrayConstructor
	| Int16ArrayConstructor
	| Int32ArrayConstructor
	| Float32ArrayConstructor

export const Constants = {
  BYTE: 5120,
  UNSIGNED_BYTE: 5121,
  SHORT: 5122,
  UNSIGNED_SHORT: 5123,
  INT: 5124,
	UNSIGNED_INT: 5125,

	FLOAT: 5126,
}

  export const arrayByType = {
	[Constants.BYTE]           : Int8Array,
	[Constants.UNSIGNED_BYTE]  : Uint8Array,
	[Constants.SHORT]          : Int16Array,
	[Constants.UNSIGNED_SHORT] : Uint16Array,
	[Constants.INT]            : Int32Array,
	[Constants.UNSIGNED_INT]   : Uint32Array,
	[Constants.FLOAT]          : Float32Array,
} as const;


const buffer = new ArrayBuffer();

// Ok
{
  const ArrayClass: TypedArrayConstructor = Int8Array;
  const c = new ArrayClass(buffer, 0, 1);
}

// Compile Error
{
  const componentType = Constants.BYTE;
  const ArrayClass: TypedArrayConstructor = arrayByType[componentType];
  const c = new ArrayClass(buffer, 0, 1); // Expected 0-1 arguments
}

🙁 Actual behavior

The last line returns a compile error.

🙂 Expected behavior

There should be no compile errors.

Additional information about the issue

No response

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.