question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

type for Function.prototype.bind wrongly preserves fields on a function

See original GitHub issue

When calling bind on a function Javascript does not copy the properties across that have been added to that function, however TS treats them as having been copied.

TypeScript Version: 3.7-Beta

Search Terms: function bind

Code

const a = () => { }
a.hello = 'hi'

const b = a.bind({})

b.hello.split("") // Uncaught TypeError: Cannot read property 'split' of undefined

Expected behavior: hello field should not be present on typeof b.

Actual behavior: hello is deemed to exist on typeof b and the program crashes.

Playground Link: http://www.typescriptlang.org/play/?ts=3.7-Beta&ssl=1&ssc=1&pln=7&pc=1#code/MYewdgzgLgBAhjAvDAFASiQPhgbxgXwCg4A6ACwFMAbKkJGAcjIEsHDDRJYAje075mAAmKHPjTtu5arRIQADlWZQUAIlUYA9JpgBVMMDgBXAOZlYAFQCe8igFEATg5AOAXDADCcMGBCwHFHBCMPLOtg5QVowKSlAMMCAAZjBGwhSJghRChEA

Related Issues: https://github.com/Microsoft/TypeScript/issues/212 seems to be where this was discussed and finally implemented, although it’s not directly related.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ilogicocommented, Oct 14, 2019

BTW, this only happens when using this specific overload.

const f = (a: boolean) => a;
f.value = 42;

const g = f.bind(undefined);
g.value; // OK!

const h = f.bind(undefined, true);
h.value; // Error, as expected
0reactions
RichieAHBcommented, Oct 31, 2019

T & was what I was missing, that looks like it would be exactly what was needed 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function.prototype.bind() - JavaScript - MDN Web Docs - Mozilla
Using bind() on classes preserves most of the class's semantics, except that all static own properties of the current class are lost. However, ......
Read more >
Preserving a reference to "this" in JavaScript prototype functions
The .bind method from Prototype.js if (!Function.prototype.bind) { // check if native implementation available Function.prototype.bind ...
Read more >
Improving Function.prototype.bind - ESDiscuss.org
Python always binds methods that you access via obj.mymethod. Multiple accesses are equal (which I think is carried over to data structures), but...
Read more >
function.prototype.bind and function.prototype.length in ...
Function Bind bind is a method on the prototype of all functions in JavaScript. It allows you to create a new function from...
Read more >
ECMA-262 Edition 5.1 - ECMAScript Language Specification
4.3.1 type; 4.3.2 primitive value; 4.3.3 object; 4.3.4 constructor; 4.3.5 prototype ... Only ECMAScript objects created using Function.prototype.bind have a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found