facebook / facebook/flow

Uint8Array.from 's `map` argument should define the type necessary for `source`

Open
#5,607 0 comments 3 reactions 0 assignees View on GitHub
Library definitions
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

When only a single argument is provided to `Uint8Array.from`, it is properly typed to allow the following [1]:

```
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
```

However, if it is provided with a 2nd `map` argument that returns one of the above, the value of each value in the array of `source` (arg 0) should correspond to the `value` argument of `map`.

Example failure for valid code [2]:

```
const base64 = 'encoded-string';
const buffer = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
```

Error output:

```
2: const buffer = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
^ string. This type is incompatible with the expected param type of
[LIB] static/v0.62.0/flowlib/core.js:635: static from(iterable: Iterable, mapFn?: (element: number) => number, thisArg?: any): this;
^ $Iterable
Property `@@iterator` is incompatible:
[LIB] static/v0.62.0/flowlib/core.js:288: @@iterator(): Iterator;
^ function type. This type is incompatible with
[LIB] static/v0.62.0/flowlib/core.js:503: @@iterator(): $Iterator;
^ function type
The return is incompatible:
[LIB] static/v0.62.0/flowlib/core.js:288: @@iterator(): Iterator;
^ $Iterator. Has some incompatible type argument with
[LIB] static/v0.62.0/flowlib/core.js:503: @@iterator(): $Iterator;
^ $Iterator
Type argument `Yield` is incompatible:
[LIB] static/v0.62.0/flowlib/core.js:288: @@iterator(): Iterator;
^ string. This type is incompatible with
[LIB] static/v0.62.0/flowlib/core.js:635: static from(iterable: Iterable, mapFn?: (element: number) => number, thisArg?: any): this;
^ number
2: const buffer = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
^ property `charCodeAt`. Property not found in
2: const buffer = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
^ Number
```

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from

[2] https://flow.org/try/#0MYewdgzgLgBARgQwgUwGwBYYF4YHJligAmyRAtNAE4CWYA5rgNwBQokscArgGbfKXYYAVVpQAHAEFKlBAE8AdN0ogAtgAoEUEHDWIUGAJQAaGMGwA+U-OAALBJQDCIEhKhqADAYOMgA

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.