Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedArray constructor compile error #61134

Open
andrewvarga opened this issue Feb 6, 2025 · 4 comments
Open

TypedArray constructor compile error #61134

andrewvarga opened this issue Feb 6, 2025 · 4 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@andrewvarga
Copy link

🔎 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

@jcalz
Copy link
Contributor

jcalz commented Feb 6, 2025

looks like a duplicate of #60745, fixed by #60934

@andrewvarga
Copy link
Author

looks like a duplicate of #60745, fixed by #60934

Thanks, sorry for not catching the existing issue.
I still wonder how the compile error arrives since ArrayClass is explicitly declared to be of type TypedArrayConstructor..somehow the assignment still changes its type?

@MartinJohns
Copy link
Contributor

I still wonder how the compile error arrives since ArrayClass is explicitly declared to be of type TypedArrayConstructor..somehow the assignment still changes its type?

Assignment narrows the type. Here's a simplified example:

const value: string | number = 123; // Always assigning a number.
value; // Variable is typed number, because the compiler knows we always assign a number.

In your Ok case the variable is narrowed to Int8Array, because the compiler can see that's what you always assign. In your error case the compiler can't know it, so it can't narrow the variable.

@andrewvarga
Copy link
Author

I still wonder how the compile error arrives since ArrayClass is explicitly declared to be of type TypedArrayConstructor..somehow the assignment still changes its type?

Assignment narrows the type. Here's a simplified example:

const value: string | number = 123; // Always assigning a number.
value; // Variable is typed number, because the compiler knows we always assign a number.
In your Ok case the variable is narrowed to Int8Array, because the compiler can see that's what you always assign. In your error case the compiler can't know it, so it can't narrow the variable.

Oh, I wasn't aware of that, interesting. I can see this being useful in many cases, I could also argue that if the developer declares a type explicitly then no further automatic narrowing should be done..I'm guessing the reason it still happens is that it can only help catch more errors, so there is no advantage not doing it.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

5 participants