Coder Social home page Coder Social logo

bind.ts's Introduction

DEPRECATED: Use --strictBindCallApply instead since TypeScript 3.2

Finally TypeScript 3.2 officially supports this feature!!!

See: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html#strictbindcallapply


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]>

bind.ts's People

Contributors

renovate-bot avatar renovate[bot] avatar teppeis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.