google / google/closure-compiler

es6.js externs for Reflect.construct specifies incorrect type for parameter "argList"

Open
#3,475 5 comments 0 reactions 0 assignees View on GitHub
internal-issue-created triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

https://github.com/google/closure-compiler/blob/v20190909/externs/es6.js#L1580

/**
* @param {function(new: ?, ...?)} targetConstructorFn
* @param {!Array} argList
* @param {function(new: TARGET, ...?)=} opt_newTargetConstructorFn
* @return {TARGET}
* @template TARGET
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct
*/
Reflect.construct = function(targetConstructorFn, argList, opt_newTargetConstructorFn) {};

The type of the param `argList` is `{!Array}` but should be `{!IArrayLike}`.

A work-around is to use `Array.from`, but this should be unnecessary. For example

/**
* @param {function(new: TARGET, ...?)} SuperCtor
* @param {function(TARGET)} callback
* @return {function(new: TARGET, ...?)}
* @template TARGET
*/
function decorate(SuperCtor, callback) {
return function DerivedCtor() {
var target = Reflect.construct(SuperCtor, Array.from(arguments), DerivedCtor);
callback(target);
return target;
};
}

Without `Array.from` the following is emitted by Closure Compiler

> WARNING - actual parameter 2 of Reflect.construct does not match formal parameter
> found : Arguments
> required: Array
> var target = Reflect.construct(SuperCtor, arguments, DerivedCtor);

Ref: https://www.ecma-international.org/ecma-262/6.0/#sec-reflect.construct
Ref: https://www.ecma-international.org/ecma-262/10.0/#sec-reflect.construct

> 26.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] )
>
> When the construct function is called with arguments target, argumentsList, and newTarget, the following steps are taken:
>
> 1. If IsConstructor(target) is false, throw a TypeError exception.
> 2. If newTarget is not present, set newTarget to target.
> 3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
> 4. **Let args be ? CreateListFromArrayLike(argumentsList).**
> 5. Return ? Construct(target, args, newTarget).

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.