-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to add a call signature to the Mapped Type OR to remove all Function.prototype
methods
#27575
Comments
type Unfunction<T> = T & { call: null; apply: null; bind: null; }
function getUnfunction<T extends Function>(callableObject: T): Unfunction<T> {
Object.setPrototypeOf(callableObject, null);
return callableObject as Unfunction<T>;
}
let callableObject = getUnfunction(() => 'foo');
callableObject() // 'foo'
callableObject // {bar: 'baz'}
callableObject.call() // <---------- error |
You could also use |
The problem with this method is that all the prototype props still exist on the type, just as 'never'. I feel there should be a way to explicitly 'mask' types out completely so that they don't appear in the type at all - for the sake of a clean api. Been talking about it here: this is kinda linked too: |
I agree with @anurbol . Sometimes we have custom property on function like @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. |
+1 would love to exclude extra these function properties to make the IDE experience better |
We could really use a feature where mapped types would not strip call and construct signatures or there would be a new mapping type that would perform mapping on call and/or construct signatures instead of (or in addition to) regular property mapping. For callables, this would be very useful for implementing any sort of callable interfaces. For newables, perhaps more common, you could perform property mappings on constructors with additional properties (classes with static fields) - without loosing the construct signature. |
I found a workaround: declare class SomeName {
static someField: any;
}
type Identity<T> = T;
interface SomeName extends Identity<typeof SomeName> {
(...args: any[]): any;
}
const someName: SomeName this solution allows |
I like @Krombik's workaround and I think this should be the default display order for properties on callable objects. A big limitation though is that I feel that it is generally a terrible idea to remove properties and methods from the function prototype just to get cleaner tooltips, unless the implementer truly has a good reason to prescribe how users should call their functions. |
@RyanCavanaugh this breaks in TS 4.3.5, where it deduces the type of Is there an existing issue for this?
|
#29261 seems to be the place for that |
Search Terms
add a call signature to the Mapped Type; remove all
Function.prototype
methodsSuggestion
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 theFunction.prototype
methods are removed from it (e.g. with setPrototypeOf..null). However IDE will still suggest theFunction.prototype
methods.Desired behavior:
Current behavior:
What I tried:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: