microsoft / microsoft/TypeScript

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

Open
#34,338 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript Playground link and reproduce the shown bind example using the reported TypeScript version. Compare the inferred type of b with JavaScript's runtime behavior; done means the added hello field is rejected on typeof b while valid bind behavior remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.