microsoft / microsoft/TypeScript

In JS, reassignment of a constructor function should work.

Open
#22,485 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug checkJs Domain: JavaScript
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

graceful-fs allows the user to provide a constructor function for the ReadStream class. It then modifies the function’s prototype (including modifying its base class). If no function is provided, then it provides one. This pattern should be supported as long as the provided function is assignable to the default one.

// @noEmit: true
// @allowJs: true
// @checkJs: true
// @Filename: a.js

var fs = require('fs')
/** @type {{ a: () => void }} */
var legStreams
// from graceful-fs
function patch(process) {
    if (process) {
        ReadStream = legStreams.a
    }
    ReadStream.prototype = Object.create(fs.ReadStream)
    ReadStream.prototype.open = fs$open

    function ReadStream (path, options) {
        if (this instanceof ReadStream) {
            return fs.ReadStream.apply(this, arguments), this
        }
        else {
            return ReadStream.apply(Object.create(ReadStream.prototype), arguments)
        }
    }
    function fs$open() {
    }

    // ReadStream should still be newable too!
    function createReadStream(path, options) {
        return new ReadStream(path, options)
    }

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 a.js reproduction in the issue and run it with allowJs and checkJs enabled. Trace how JavaScript constructor functions are typed when ReadStream is reassigned, including the later new ReadStream call. Done means the repro accepts an assignable reassigned constructor while preserving its newability.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.