Repository navigation
Incorrect constructor overload: takes only first overload, but not othersΒ #60745
Copy link
Copy link
Closed
Closed
Copy link
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
π Search Terms
constructor, typescript, Uint32Array, Uint16Array, Uint8Array, Uint32ArrayConstructor, Uint16ArrayConstructor, Uint8ArrayConstructor
π Version & Regression Information
- This changed between versions 5.6.3 and 5.7.2
β― Playground Link
π» Code
type IndexElementsConstructor = Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor;
function test(componentType: number, chunkDataBin: ArrayBuffer) {
let viewType: IndexElementsConstructor;
switch (componentType) {
case WebGL2RenderingContext.UNSIGNED_INT:
viewType = Uint32Array;
break;
case WebGL2RenderingContext.UNSIGNED_SHORT:
viewType = Uint16Array;
break;
case WebGL2RenderingContext.UNSIGNED_BYTE:
viewType = Uint8Array;
break;
default:
throw new Error(`"componentType" is not UNSIGNED_INT, UNSIGNED_SHORT or UNSIGNED_BYTE (${componentType})`);
}
const calculatedBufferOffset = 4;
const calculatedBufferLength = 8;
return new viewType(chunkDataBin, calculatedBufferOffset, calculatedBufferLength);
}π Actual behavior
π Expected behavior
Additional information about the issue
If change code to simpler variant:
type IndexElementsConstructor = Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor;
function test(componentType: number, chunkDataBin: ArrayBuffer) {
const viewType: IndexElementsConstructor = Uint32Array;
const calculatedBufferOffset = 4;
const calculatedBufferLength = 8;
return new viewType(chunkDataBin, calculatedBufferOffset, calculatedBufferLength);
}newest TS version recognizes this.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed


Activity
psnet commentedon Dec 12, 2024
As we can see 3rd overload:
Microsoft VS Code\resources\app\extensions\node_modules\typescript\lib\lib.es5.d.ts:jcalz commentedon Dec 12, 2024
This isn't a bug in TS, but it's unfortunate fallout of the new feature whereby
TypedArrays are generic overArrayBufferLikeas implemented in #59417. You're running afoul of one of the caveats in the the support for calling union types implemented in #29011:So since the new constructors are generic, they have type parameters, and things go sideways. They wanted #59417 not to be a breaking change but I think there are these edge cases where it happens. You should probably change
IndexElementsConstructortoto make it work again.
Playground link to code
rubick24 commentedon Dec 13, 2024
Iβm experiencing same problem in ts 5.7 when extending TypedArray class like this:
https://www.typescriptlang.org/play/?target=99#code/KYDwDg9gTgLgBAYwDYEMDOa4A06hsAOwBNMAxJCFGAZgCYBBKKFATzgG8AoORCAtGFACuCGNAAUMAFxwADHAA+cAIyK4tAJQduPOGgDuASxgIAFnElauu3QnTA5UnTZ5ohYYFHHKNzlwCMoYBQAaz8eOzQHZScXXTcPLwJgfThGZhYAISEAMxzPcQ0AGjkSn3DdQOCwuKJgHJQhJGkK13cC5NSAZVMUIKJ01my8guLSlV84nirQvwBfHQWFoA
typescript-bot commentedon Dec 16, 2024
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
psnet commentedon Dec 16, 2024
Joe Calzaretta (@jcalz) thanks for explaining. Sadly that during #59417 were not implemented better solutions and when for union types caveats. Because of that all together code in the example above feels like tricky. Or you need to write code that will satisfy TS, code for TS, not for you π’
psnet commentedon Dec 16, 2024
Now if we want
BYTES_PER_ELEMENTfromthings become more complicated.
psnet commentedon Dec 16, 2024
Marking not interesting issues with "Design Limitation" is very interesting tool for solving problems, is not it, Ryan Cavanaugh (@RyanCavanaugh) ?
Somebody failed once long time ago, someone failed again now and we got mess, but instead of fix "we will close ticket and act like nothing happens"?
RyanCavanaugh commentedon Dec 16, 2024
Serhii Pustovit (@psnet) See https://github.com/Microsoft/TypeScript/wiki/FAQ#this-is-closed-but-should-be-open-or-vice-versa
rbuckton commentedon Jan 8, 2025
When you union constructor types, generic signatures must match exactly. Since each constructor type in the union produces a different return type, they are excluded from the resulting set. In the long term, I can look into adding non-generic overloads for these cases, assuming its feasible to do so without introducing further breaks. In the short term, Joe Calzaretta (@jcalz) suggestion is a good workaround.