question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to add a call signature to the Mapped Type OR to remove all `Function.prototype` methods

See original GitHub issue

Search Terms

add a call signature to the Mapped Type; remove all Function.prototype methods

Suggestion

Being able to create a callable type, but without Function.prototype methods.

Use Cases

Imagine one creates some API that exposes a function that is (because function is an object) also have some custom properties. This function is not supposed to be call-ed, apply-ed, bind-ed, etc, in fact all the Function.prototype methods are removed from it (e.g. with setPrototypeOf…null). However IDE will still suggest the Function.prototype methods.

Desired behavior:

let callableObject = () => 'foo'
Object.setPrototypeOf(callableObject, null)
callableObject.bar = 'baz'

callableObject() // 'foo'
callableObject // {bar: 'baz'}
callableObject.call // <---------- error

Current behavior:

let callableObject = () => 'foo'
Object.setPrototypeOf(callableObject, null)
callableObject.bar = 'baz'

callableObject() // 'foo'
callableObject // {bar: 'baz'}
callableObject.call // <---------- OK

What I tried:

type ExcludeFunctionPrototypeMethods<T extends () => any> = {
    [K in Exclude<keyof T, keyof Function>]: T[K]
}
// the type above "lacks a call signature" and I have no Idea how to add it there.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript / JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. new expression-level syntax)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:11
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
fwh1990commented, Jul 10, 2020

I agree with @anurbol . Sometimes we have custom property on function like fn.foo = 123, and we want user see this property from IDE tips, however, so many unexpected properties like apply | call | bind | arguments ... are shown, in other words fn.foo is hidden from them, user is very hard to find custom things in a second.

@RyanCavanaugh Maybe this issue should not be closed.

3reactions
nth-commitcommented, Sep 15, 2020

+1

Trying to create a concise function builder, and having the interesting methods polluted by the function prototype.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: how to add a call signature to the Mapped Type?
I add methods dynamically, so I have to use Mapped Type for the function. So my code looks like this, and it does...
Read more >
Typing functions • Tackling TypeScript - Exploring JS
The interface member in line A is a call signature. It looks similar to a method signature, but doesn't have a name. The...
Read more >
Mapped type/wrapped Type without losing call signature ...
Is there any possible way to wrap a type that has multiple call signatures and have the wrapped type have those same call...
Read more >
Documentation - Advanced Types - TypeScript
Signature '(pet: Fish | Bird): boolean' must be a type predicate. ... the type of the function's prototype property if its type is...
Read more >
Array.prototype.pop() - JavaScript - MDN Web Docs
The pop() method is a mutating method. It changes the length and the content of this . In case you want the value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found