Skip to content

Commit

Permalink
Improve typing of Symbol.iterator
Browse files Browse the repository at this point in the history
Summary: Previously, `Symbol.iterator` was just typed as `string`. Now, it is typed with the polyfilled value (that we also use internally). Maybe one day in the future we can actually use symbols as property keys, but until then this is an improvement from the previous state.

Reviewed By: mroch

Differential Revision: D27611562

fbshipit-source-id: bf31193eb353a532dbeb2eb1ba397fb57fe18097
  • Loading branch information
gkz authored and facebook-github-bot committed Apr 7, 2021
1 parent ad6424b commit 1757859
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/core.js
Expand Up @@ -278,7 +278,7 @@ declare class Symbol {
* by Array.prototype.concat.
*/
static isConcatSpreadable: $SymboIsConcatSpreadable;
static iterator: string; // polyfill '@@iterator'
static iterator: '@@iterator'; // polyfill '@@iterator'
/**
* Returns a key from the global symbol registry matching the given Symbol if found.
* Otherwise, returns a undefined.
Expand Down
6 changes: 6 additions & 0 deletions tests/symbol/iterator.js
@@ -0,0 +1,6 @@
declare var foo: {
@@iterator: Iterator<number>,
};

(foo[Symbol.iterator]: Iterator<number>); // OK
(foo[Symbol.iterator]: boolean); // Error
20 changes: 19 additions & 1 deletion tests/symbol/symbol.exp
@@ -1,3 +1,21 @@
Error -------------------------------------------------------------------------------------------------- iterator.js:6:2

Cannot cast `foo[Symbol.iterator]` to boolean because `$Iterator` [1] is incompatible with boolean [2].
[incompatible-cast]

iterator.js:6:2
6| (foo[Symbol.iterator]: boolean); // Error
^^^^^^^^^^^^^^^^^^^^

References:
iterator.js:2:15
2| @@iterator: Iterator<number>,
^^^^^^^^^^^^^^^^ [1]
iterator.js:6:24
6| (foo[Symbol.iterator]: boolean); // Error
^^^^^^^ [2]


Error --------------------------------------------------------------------------------------------------- symbol.js:8:17

Cannot call `Symbol` because no more than 1 argument is expected by function type [1]. [extra-arg]
Expand Down Expand Up @@ -137,4 +155,4 @@ References:



Found 8 errors
Found 9 errors

0 comments on commit 1757859

Please sign in to comment.