Typescript: "2 definitions" when using module.exports to export a function wrapped in {}

Created on 6 Nov 2018  路  5Comments  路  Source: microsoft/TypeScript

  • VSCode Version: 1.28.2
  • OS Version: macOS 10.14

I noticed there are several issues opened against "2 definitions", noticeably https://github.com/Microsoft/vscode/issues/51459 & https://github.com/Microsoft/TypeScript/issues/24861

But I don't think my issue is the same (I don't use ts or react, just nodejs code). It happens when I export like this

function downloadVoice() {
...
}

module.exports = {
    downloadVoice
}
// If I export like this it will not happen
// module.exports = downloadVoice

But when I hover over to the place when the function is called, vscode can show the definition correctly,

2 inspection

The 2 definitions warning:

ks3test_js_ _inspection


Does this issue occur when all extensions are disabled?: Yes

Bug JavaScript

Most helpful comment

Hi I believe this bug has been there for a long time and it is quite annoying. Do you have plan to fix it recently ?

All 5 comments

I can't repo this in a simple project:

x.js:

function downloadVoice() {

}

module.exports = {
    downloadVoice
}
// If I export like this it will not happen
// module.exports = downloadVoice

y.js:

const { downloadVoice } = require('./x') 

downloadVoice

Please test using the code 1.29. If you still see this, please provide a complete code example that shows this problem

Hi it is very easy to reproduce

First use express generator to create a simple project, then change router/index.js to this

var express = require("express");
var router = express.Router();

/* GET home page. */
router.get("/", function(req, res, next) {
  res.render("index", { title: "Express" });
});

function hi() {
  console.log("hi");
}

module.exports = { router, hi };

And in app.js import like this

var dup = require("./routes/index");
var usersRouter = require("./routes/users");
dup.hi();

Then click dup.hi() to see the warning.

I tested 1.29, it still has the problem.

Confirmed with TS 3.2.0-dev.20181114

Hi I believe this bug has been there for a long time and it is quite annoying. Do you have plan to fix it recently ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blakeembrey picture blakeembrey  路  171Comments

chanon picture chanon  路  138Comments

fdecampredon picture fdecampredon  路  358Comments

disshishkov picture disshishkov  路  224Comments

born2net picture born2net  路  150Comments