Closed
Description
Found when debugging a different mysterious commonjs bug with @elibarzilay:
- Install @types/node for this repro. I couldn't get it to repro on a local module, but I think I'm missing something fairly simple.
// @filename: ns.ts
namespace myAssert {
export type cool = 'cool'
}
var myAssert = require('assert')
// @filename: test.js
exports.equal = myAssert.equal
exports.equal()
🙁 Actual behavior
exports.equal: (actual: any, expected: any, message: string | Error | undefined): void
- Error on
exports.equal()
: expected 3 arguments. - Also, goto-def on
exports.equal
goes to the definition ofequal
inasserts
.
🙂 Expected behavior
exports.equal: any
- No error on
exports.equal()
- No goto-def on
exports.equal
Discussion
var x = require
is not supposed to work in TS. However, resolveEntityName
checks whether the wrong node is in a JS file before attempting to resolve require
calls:
if (isInJSFile(name)) {
if (namespace.valueDeclaration && ......
name
is the name being resolved, but namespace
is the one being checked for require
. The first line should be if (isInJSFile(namespace.valueDeclaration))
. Also the two if
should be combined into one.
This is an easy fix -- the hard part is making a standalone test that fails before the fix.