Skip to content

In JS, export assignments should bind as types, not just values #23788

Open
@sandersn

Description

@sandersn

Currently, there is special code in the checker to look up values as type in order to support the value-only way that the binder exports things that come from an export assignment, even things that in Typescript export types, such as classes.

The binder should instead treat JS export assignments as types if the initialiser (such as a class or constructor function) or later assignments (such as prototype assignments) warrant it. That means the checker code will be more uniform and easier to understand.

Code

function F() {
}
F.prototype.method = function() {
}
exports.F = F

Allows F to be used as a type:

var x = require('./module')
/** @type {x.F} */

But this doesn't work in pure typespace:

/** @typedef {import('./module').F} F */
/** @type {F} */
var f;

Note that binding this way will enable tricks that aren't possible in Typescript today:

module.exports. C = class {
}
module.exports.C.D = class {
}

Now var m = require('./module') makes both m.C and m.C.D available as types.

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