You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expected behavior:
I'd expect the constructor for class Broken to not throw an error, as TypedArray can accept both numbers or TypedArray instances. From the MDN docs:
newTypedArray();// new in ES2017newTypedArray(length);newTypedArray(typedArray);newTypedArray(object);newTypedArray(buffer[,byteOffset[,length]]);whereTypedArray()isoneof:
Int8Array();Uint8Array();Uint8ClampedArray();Int16Array();Uint16Array();Int32Array();Uint32Array();Float32Array();Float64Array();
Actual behavior:
The constructor for class Broken throws a type error:
Argument of type 'number | Float32Array' is not assignable to parameter of type 'ArrayLike<number> | ArrayBuffer'.
Type 'number' is not assignable to type 'ArrayLike<number> | ArrayBuffer'.
(parameter) arg: number | Float32Array
This type error can be circumvented by wrapping duplicate versions of the same code in a typeguard, as shown in the second class Works example.
TypeScript Version: 2.6.1-insiders.20171019
Search Terms:
TypedArray Float32Array ArrayBufferView constructor overload
Code
Expected behavior:
I'd expect the constructor for
class Broken
to not throw an error, asTypedArray
can accept both numbers orTypedArray
instances. From the MDN docs:Actual behavior:
The constructor for
class Broken
throws a type error:This type error can be circumvented by wrapping duplicate versions of the same code in a typeguard, as shown in the second
class Works
example.Playground Link: https://www.typescriptlang.org/play/index.html#src=class%20Broken%20%7B%0D%0A%20%20length%3A%20number%3B%0D%0A%20%20array%3A%20Float32Array%3B%0D%0A%20%20constructor(arg%3A%20number%20%7C%20Float32Array)%20%7B%20%0D%0A%20%20%20%20this.array%20%3D%20new%20Float32Array(arg)%20%2F%2F%20arg%20throws%20error%0D%0A%20%20%20%20this.length%20%3D%20this.array.length%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Works%20%7B%0D%0A%20%20length%3A%20number%3B%0D%0A%20%20array%3A%20Float32Array%3B%0D%0A%20%20constructor(arg%3A%20number%20%7C%20Float32Array)%20%7B%20%0D%0A%20%20%20%20if%20(typeof%20arg%20%3D%3D%3D%20'number')%20%7B%0D%0A%20%20%20%20%20%20this.array%20%3D%20new%20Float32Array(arg)%0D%0A%20%20%20%20%20%20this.length%20%3D%20this.array.length%0D%0A%20%20%20%20%7D%20else%20%7B%0D%0A%20%20%20%20%20%20this.array%20%3D%20new%20Float32Array(arg)%0D%0A%20%20%20%20%20%20this.length%20%3D%20this.array.length%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%7D
Related Issues:
Suggestion: a built-in TypedArray interface
Empty constructors for TypedArrays are not allowed
TypedArrays missing signature
The text was updated successfully, but these errors were encountered: