type for Function.prototype.bind wrongly preserves fields on a function
See original GitHub issueWhen 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.
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:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
BTW, this only happens when using this specific overload.
T &
was what I was missing, that looks like it would be exactly what was needed 👍