Skip to content

JS: Name resolution of classes is broken #32993

Closed
@sandersn

Description

@sandersn

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Code

Run this:

$ npm install @types/node

Have a tsconfig like this:

{
    "compilerOptions": {
        "noEmit": true,
        "strict": true,
        "target": "esnext",
        "lib": ["es5", "es2015", "es2016", "es2017", "esnext", "dom"],
        "allowJs": true,
        "checkJs": true,
        "outDir": "out",
        "jsx": "preserve",
        "esModuleInterop": true,
        "module": "commonjs",
    },
    "files": [
        "welove.ts",
        "mod1.js"
    ]
}

In mod1.js:

const EventEmitter = require('events')
class Worker extends EventEmitter {
}

module.exports = {Worker};

In welove.ts:

import { Worker } from './mod1'
function f(w: Worker) {
}
var wp = new Worker()
f(wp)

Expected behavior:
type reference and value reference to Worker refer to the class defined in mod1.js.

Actual behavior:
type reference Worker refers to the one in lib.dom.d.ts.

Two variants:

  1. If you convert mod1.js to typescript with the equivalent export { Worker }, the error goes away.
  2. If you use the syntax module.exports.Worker = class Worker extends EventEmitter, the error goes away, but goto-def on the value Worker throws an assertion:
Error: Debug Failure. Expected declaration to have at least one class-like declaration
    at getConstructSignatureDefinition (/home/nathansa/ts/built/local/tsserver.js:107575:89)
    at getDefinitionFromSymbol (/home/nathansa/ts/built/local/tsserver.js:107570:20)
    at Object.getDefinitionAtPosition (/home/nathansa/ts/built/local/tsserver.js:107439:20)
    at Object.getDefinitionAtPosition (/home/nathansa/ts/built/local/tsserver.js:125773:38)

Activity

sandersn

sandersn commented on Aug 20, 2019

@sandersn
MemberAuthor

@weswigham I'm assigning to you because I'm pretty sure this is a result of #32947. If it's actually a result of contructor-functions-as-classes, re-assign to me.

added this to the TypeScript 3.7.0 milestone on Aug 20, 2019
weswigham

weswigham commented on Aug 20, 2019

@weswigham
Member

@sandersn this was broken in TS 3.5, too (and probably stretches back even farther)~

image

On inspection, yeah - also broken in 3.1.

sandersn

sandersn commented on Aug 21, 2019

@sandersn
MemberAuthor

It's a duplicate of commonjs-exports-should-be-aliases, #25533, although this is a particularly good example of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already created

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @sandersn@weswigham

      Issue actions

        JS: Name resolution of classes is broken · Issue #32993 · microsoft/TypeScript