Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

bind.ts

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Issues
File Explorer

Advanced tools

bind.ts

Function.prototype.bind with typings for TypeScript

    2.0.0latest

Version published
Maintainers
1
Yearly downloads
3,950
increased by345.82%

Weekly downloads

Readme

Source

bind.ts

Function.prototype.bind with typings for TypeScript

npm version Node.js Version Support build status dependency status License

Function#bind is untyped in TypeScript (in v3.0 at least, cf: #212). It always returns any.

function foo(a: number, b: number): string { return `${this.msg}: ${a + b}`; } // bar is `any`! const bar = foo.bind({msg: 'hello'}, 10); // ok bar(20); // 'hello: 30' // should be a type error, but actually allowed... bar('anything');

bind.ts is a workaround for this issue.

import bind from 'bind.ts'; // bar is `(b: number) => string` const bar = bind(foo, {msg: 'hello'}, 10); // ok bar(20); // 'hello: 30' // error! bar('anything');

Note: TypeScript v3.0 introduces generic rest parameters. It's helpful but cannot solve this issue completely and the type definition of Function#bind in TypeScript core is not changed.

Requirement

bind.ts v2 requires TypeScript v3.0+. For TypeScript v2.x, use bind.ts v1.

Limitation

The maximum number of params of bind() is 5.

License

MIT License: Teppei Sato <[email protected]>

Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc