You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Strict Reflect.apply Reflect.construct and Function.prototype.apply/call/bind #33889
functiongetString(){return'string'}classClassA{}Reflect.apply(getString,null,[])// expected string but got anyReflect.construct(ClassA,[])// expected ClassA but got anyFunction.prototype.apply.call(getString,undefined)// expected string but got anyFunction.prototype.call.call(getString,undefined)// expected string but got anyFunction.prototype.bind.call(getString,undefined)()// expected string but got any
I'd expect Function.prototype.apply(null, getString); to error, like (Function.prototype as CallableFunction).apply(null, getString);:
error TS2684: The 'this' context of type 'CallableFunction' is not assignable to method's 'this' of type '(this: any, ...args: any[]) => unknown'.
Type 'CallableFunction' provides no match for the signature '(this: any, ...args: any[]): unknown'.
--stringBindCallApply should apply the CallableFunction interface to the Function global as well as to function instances?
The original use case code has an error and has been fixed.
Since Reflect.apply is easier to use than Function.prototype.apply and Reflect.construct provides feature that Function.prototype does not have, maybe we don't need to apply CallableFunction interface to Function, we just need to improve Reflect.apply and Reflect.construct.
This issue is equivalent to say: add type guard for Reflect.apply under --strictBindCallApply.
Example:
functionfoo(name: string){console.log(name)}foo.apply(null,[42]);// typescript will report type error, great!Function.prototype.apply.call(foo,null,[42]);// no error 😢Reflect.apply(foo,null,[42]);// no error 😢
Activity
jablko commentedon Oct 9, 2019
I'd expect
Function.prototype.apply(null, getString);to error, like(Function.prototype as CallableFunction).apply(null, getString);:--stringBindCallApplyshould apply theCallableFunctioninterface to theFunctionglobal as well as to function instances?BlackGlory commentedon Oct 10, 2019
The original use case code has an error and has been fixed.
Since
Reflect.applyis easier to use thanFunction.prototype.applyandReflect.constructprovides feature thatFunction.prototypedoes not have, maybe we don't need to applyCallableFunctioninterface toFunction, we just need to improveReflect.applyandReflect.construct.jablko commentedon Oct 10, 2019
Just checking whether the spread operator available to you?
BlackGlory commentedon Oct 11, 2019
This is not an issue about how to dynamically create an object instance.
jablko commentedon Oct 11, 2019
Would you consider adding more information about your use case?
zanminkian commentedon Sep 16, 2024
This issue is equivalent to say: add type guard for
Reflect.applyunder--strictBindCallApply.Example: