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 overload behaves incorrectly(?) #21437

Closed
alexburner opened this issue Jan 27, 2018 · 2 comments
Closed

TypedArray constructor overload behaves incorrectly(?) #21437

alexburner opened this issue Jan 27, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@alexburner
Copy link

alexburner commented Jan 27, 2018

TypeScript Version: 2.6.1-insiders.20171019

Search Terms:
TypedArray Float32Array ArrayBufferView constructor overload

Code

class Broken {
  length: number;
  array: Float32Array;
  constructor(arg: number | Float32Array) { 
    this.array = new Float32Array(arg) // arg throws error
    this.length = this.array.length
  }
}

class Works {
  length: number;
  array: Float32Array;
  constructor(arg: number | Float32Array) { 
    if (typeof arg === 'number') {
      this.array = new Float32Array(arg)
      this.length = this.array.length
    } else {
      this.array = new Float32Array(arg)
      this.length = this.array.length
    }
  }
}

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:

new TypedArray(); // new in ES2017
new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
new TypedArray(buffer [, byteOffset [, length]]);

where TypedArray() is one of:

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.

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

@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #14107, where you want to see if a union type can satisfy each overload.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jan 27, 2018
@alexburner
Copy link
Author

Whoops, you are correct, I got caught up in the TypedArray and missed the deeper "overload + union arguments" angle.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants