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

Better signature for callable array #7917

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ declare class Array<T> extends $ReadOnlyArray<T> {

[key: number]: T;
length: number;
static (...values:Array<any>): Array<any>;
static (length: number): Array<T>;
static <U>(...values: Array<U>): Array<U>;
static isArray(obj: mixed): bool;
static from<A, B>(iter: Iterable<A>, mapFn: (elem: A, index: number) => B, thisArg?: any): Array<B>;
static from<A>(iter: Iterable<A>, mapFn: void): Array<A>;
Expand Down
5 changes: 5 additions & 0 deletions tests/arraylib/array_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var l: Array<number> = h.concat(1,2,3);
var m: Array<number | string> = h.concat('a', 'b', 'c');
var n: Array<number> = h.concat('a', 'b', 'c'); // Error

var o: Array<string> = Array('a', 'b')
var p: Array<number> = Array('a', 'b') // error, string ~> number
var q: Array<string> = Array(0) // ok
var r: Array<number> = Array(20) // ok

function reduce_test() {
/* Adapted from the following source:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Expand Down
103 changes: 73 additions & 30 deletions tests/arraylib/arraylib.exp
Original file line number Diff line number Diff line change
Expand Up @@ -75,84 +75,127 @@ References:
^^^ [4]


Error ----------------------------------------------------------------------------------------------- array_lib.js:46:29
Error ----------------------------------------------------------------------------------------------- array_lib.js:24:24

Cannot assign `Array(...)` to `p` because:
- string [1] is incompatible with number [2] in array element.
- string [3] is incompatible with number [2] in array element.

array_lib.js:24:24
24| var p: Array<number> = Array('a', 'b') // error, string ~> number
^^^^^^^^^^^^^^^

References:
array_lib.js:24:30
24| var p: Array<number> = Array('a', 'b') // error, string ~> number
^^^ [1]
array_lib.js:24:14
24| var p: Array<number> = Array('a', 'b') // error, string ~> number
^^^^^^ [2]
array_lib.js:24:35
24| var p: Array<number> = Array('a', 'b') // error, string ~> number
^^^ [3]


Error ----------------------------------------------------------------------------------------------- array_lib.js:51:29

Cannot perform arithmetic operation because string [1] is not a number.

array_lib.js:46:29
46| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:51:29
51| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
^^^

References:
array_lib.js:46:4
46| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:51:4
51| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
^^ [1]


Error ----------------------------------------------------------------------------------------------- array_lib.js:46:35
Error ----------------------------------------------------------------------------------------------- array_lib.js:51:35

Cannot get `str.length` because property `length` is missing in `Number` [1].

array_lib.js:46:35
46| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:51:35
51| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
^^^^^^^^^^

References:
array_lib.js:46:29
46| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:51:29
51| [""].reduce((acc, str) => acc * str.length); // error, string ~> number
^^^^^^^^^^^^^^^^ [1]


Error ----------------------------------------------------------------------------------------------- array_lib.js:47:34
Error ----------------------------------------------------------------------------------------------- array_lib.js:52:34

Cannot perform arithmetic operation because string [1] is not a number.

array_lib.js:47:34
47| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:52:34
52| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
^^^

References:
array_lib.js:47:4
47| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:52:4
52| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
^^ [1]


Error ----------------------------------------------------------------------------------------------- array_lib.js:47:40
Error ----------------------------------------------------------------------------------------------- array_lib.js:52:40

Cannot get `str.length` because property `length` is missing in `Number` [1].

array_lib.js:47:40
47| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:52:40
52| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
^^^^^^^^^^

References:
array_lib.js:47:34
47| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
array_lib.js:52:34
52| [""].reduceRight((acc, str) => acc * str.length); // error, string ~> number
^^^^^^^^^^^^^^^^ [1]


Error ----------------------------------------------------------------------------------------------- array_lib.js:63:48
Error ----------------------------------------------------------------------------------------------- array_lib.js:68:48

Cannot assign `Array.of(...)` to `incompatibleTypeNotOkay` because:
- number [1] is incompatible with string [2] in array element.
- number [3] is incompatible with string [2] in array element.

array_lib.js:63:48
63| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
array_lib.js:68:48
68| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
^^^^^^^^^^^^^^

References:
array_lib.js:63:57
63| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
array_lib.js:68:57
68| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
^ [1]
array_lib.js:63:38
63| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
array_lib.js:68:38
68| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
^^^^^^ [2]
array_lib.js:63:60
63| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
array_lib.js:68:60
68| var incompatibleTypeNotOkay: Array<string> = Array.of(1, 2);
^ [3]


Error ----------------------------------------------------------------------------------------------- issue_7887.js:7:15

Cannot assign object literal to `a` because number [1] is incompatible with string [2] in array element of property
`prop`.

issue_7887.js:7:15
v
7| var a: Plop = {
8| prop: Array(5).fill(1) // error
9| };
^

References:
issue_7887.js:8:23
8| prop: Array(5).fill(1) // error
^ [1]
issue_7887.js:4:9
4| prop: string[]
^^^^^^ [2]


Error ---------------------------------------------------------------------------------------------------- length.js:7:1

Cannot assign `6` to `r.length` because property `length` is not writable.
Expand All @@ -170,4 +213,4 @@ Cannot assign `7` to `t.length` because property `length` is not writable.



Found 14 errors
Found 17 errors
9 changes: 9 additions & 0 deletions tests/arraylib/issue_7887.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* @flow */

type Plop = {|
prop: string[]
|};

var a: Plop = {
prop: Array(5).fill(1) // error
};