microsoft / microsoft/TypeScript

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

Open
#27,575 13 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the callable-object examples and the ExcludeFunctionPrototypeMethods mapped type in the issue, then read the comment thread for prior discussion. Determine whether the type system can preserve a call signature while excluding Function.prototype members. Done means the proposed type can be expressed and produces an error for callableObject.call while retaining callableObject().

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.