Skip to content

In JS, reassignment of a constructor function should work.  #22485

Open
@sandersn

Description

@sandersn

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)
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JavaScriptThe issue relates to JavaScript specificallycheckJsRelates to checking JavaScript using TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions