Title: Proposal: Overload Function.bind for when no argArray is provided. · Issue #22669 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Overload Function.bind for when no argArray is provided. · Issue #22669 · microsoft/TypeScript
X Title: Proposal: Overload Function.bind for when no argArray is provided. · Issue #22669 · microsoft/TypeScript
Description: Background Currently the Function.bind type definition defines only one signature: bind(this: Function, thisArg: any, ...argArray: any[]): any; This destroys typing information on the bound function. Given difficulties in generating a ne...
Open Graph Description: Background Currently the Function.bind type definition defines only one signature: bind(this: Function, thisArg: any, ...argArray: any[]): any; This destroys typing information on the bound functio...
X Description: Background Currently the Function.bind type definition defines only one signature: bind(this: Function, thisArg: any, ...argArray: any[]): any; This destroys typing information on the bound functio...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22669
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Overload Function.bind for when no argArray is provided.","articleBody":"# Background\r\n\r\nCurrently the `Function.bind` type definition defines only one signature:\r\n\r\n```typescript\r\nbind(this: Function, thisArg: any, ...argArray: any[]): any;\r\n```\r\n\r\nThis destroys typing information on the bound function. Given difficulties in generating a new type for the bound function (the number of arguments has changed) this makes sense. However, it's very common to bind functions without using the `argArray` argument to `bind`. In this case a better solution can be made.\r\n\r\n# Proposal\r\n\r\nI propose that `bind` be overloaded with a second signature:\r\n\r\n```typescript\r\nbind\u003cT extends Function>(this: T, thisArg: any): T;\r\nbind(this: Function, thisArg: any, ...argArray: any[]): any;\r\n```\r\n\r\n# Example\r\n\r\nTo use a real world example on how this improves TypeScript I'll pull something from a personal project.\r\n\r\nTypes used:\r\n\r\n```typescript\r\ntype SocketMsg = Guess | StateMsg;\r\ntype ChannelCallback\u003cT> = (payload: T, ref: Ref, joinRef: Ref) => any;\r\n\r\nexport class Channel\u003cT> {\r\n // ...\r\n on(event: ChannelEvent, callback: ChannelCallback\u003cT>): void;\r\n // ...\r\n}\r\n```\r\n\r\n```typescript\r\nexport default class GuessView extends Component\u003cGuessProps, GuessState> {\r\n channel: Channel\u003cSocketMsg>;\r\n\r\n connectSocket() {\r\n ...\r\n this.channel.on(\"guess\", this.gameGuess.bind(this));\r\n }\r\n\r\n gameGuess(guess: Guess) {\r\n let newState = Object.assign({}, this.state);\r\n newState.guesses.push(guess);\r\n this.setState(newState);\r\n }\r\n}\r\n```\r\n\r\nUnder the current `bind` signature a change to `gameGuess` such that it accepts a different type would produce no error:\r\n\r\n```typescript\r\n connectSocket() {\r\n ...\r\n this.channel.on(\"guess\", this.gameGuess.bind(this)); // Now being provided the wrong type of function\r\n }\r\n gameGuess(guess: number) {\r\n // ...\r\n```\r\n\r\nBut with `bind` utilizing a special definition for the case of only using the `thisArg` argument - the above example would produce an error.\r\n\r\n> Argument of type '(guess: number) => void' is not assignable to parameter of type 'ChannelCallback\u003cSocketMsg>'. Types of parameters 'guess' and 'payload' are incompatible. Type 'SocketMsg' is not assignable to type 'number'. Type 'Guess' is not assignable to type 'number'.","author":{"url":"https://github.com/danthedaniel","@type":"Person","name":"danthedaniel"},"datePublished":"2018-03-17T04:42:30Z","url":"https://github.com/microsoft/TypeScript/issues/22669"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:34cbf362-137e-ae0a-1841-1e9b8118acbc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDFE:F4D72:A05699:DC3128:6B0451AB |
| html-safe-nonce | 86a02ab19735674eada07bb137f5e051a629f52d37a0ff46e4029e150a822a4a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREZFOkY0RDcyOkEwNTY5OTpEQzMxMjg6NkIwNDUxQUIiLCJ2aXNpdG9yX2lkIjoiMTQ2MzM4MTQ1MjE2MjA5MzQ4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f3a25c279330d93c949fcd80b8a7b20208fbdfc1a7c405db72a663dd0422ebae |
| hovercard-subject-tag | issue:306127825 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/22669/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9975397187c71da338d6028f268793ee960a3fd14cf1f3fb0194aeedf3d06a10/microsoft/TypeScript/issues/22669 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9975397187c71da338d6028f268793ee960a3fd14cf1f3fb0194aeedf3d06a10/microsoft/TypeScript/issues/22669 |
| og:image:alt | Background Currently the Function.bind type definition defines only one signature: bind(this: Function, thisArg: any, ...argArray: any[]): any; This destroys typing information on the bound functio... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danthedaniel |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width