Allow to add a call signature to the Mapped Type OR to remove all `Function.prototype` methods
See original GitHub issueSearch 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:
- Created 5 years ago
- Reactions:11
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 likeapply | call | bind | arguments ...
are shown, in other wordsfn.foo
is hidden from them, user is very hard to find custom things in a second.@RyanCavanaugh Maybe this issue should not be closed.
+1
Trying to create a concise function builder, and having the interesting methods polluted by the function prototype.