module not resolved in JS file using var x = require('...') syntax #11825
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issueVS Code TrackedThere is a VS Code equivalent to this issueThere is a VS Code equivalent to this issue
Milestone
Description
From Tim York (@timty) on October 19, 2016 17:18
Intellisense works great for my postgress Pool type when explicitly declared:
However when passed as a parameter to a function and described through a JSDoc parameter intellisense fails:
- VSCode Version: Code 1.6.1 (9e4e44c19e393803e2b05fe2323cf4ed7e36880e, 2016-10-13T15:27:10.246Z)
- OS Version: Darwin x64 15.6.0
- Extensions: Unity.unity-debug-1.0.1, benjaminromano.typings-installer-0.2.0, dbaeumer.vscode-eslint-1.0.7, dkundel.vscode-npm-source-0.0.3, joelday.docthis-0.3.4, mohsen1.prettify-json-0.0.3, ms-vscode.omnisharp-0.3.3, rafaelmaiolla.diff-0.0.1, stevencl.addDocComments-0.0.8, xabikos.JavaScriptSnippets-1.1.0, xabikos.ReactSnippets-1.1.1
Copied from original issue: microsoft/vscode#14008
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issueVS Code TrackedThere is a VS Code equivalent to this issueThere is a VS Code equivalent to this issue


Activity
waderyan commentedon Oct 24, 2016
Tim York (@timty) thank you for opening this issue. I have confirmed this with the latest bits and no extensions. Migrating to JavaScript language service.
Code to copy and paste.
sandersn commentedon Oct 24, 2016
Wade Anderson (@waderyan) your code doesn't repro for me on master. Intellisense works fine when I paste the code in a javascript file.
Intellisense does not work for me in a TypeScript file, but Typescript doesn't rely on jsdoc to give types to parameters. It only uses the documentation.
--noImplicitAnyturned on, I get an error at zoo(x).@param {Foo} x documentation, you will see "documentation" in the quick info hover forx, indicating that Intellisense is actually working in typescript too.Tim York (@timty) Are you using typescript or javascript? In a typescript file you will not get intellisense for the above reason. In a javascript file the code you gave will not work either, because jsdoc requires the parameter name to match:
If you're using typescript, then you have to use the postfix type syntax:
function myFunction(pool2: Pool) {timty commentedon Oct 25, 2016
Nathan Shively-Sanders (@sandersn) Wade Anderson (@waderyan)
Thank you both for your responses. I am using javascript (nodejs). Sorry about the typo when recreating the issue. This does not work with correctly matching names either.
Intellisense here:
No Intellisense with JSDoc type:
No Intellisense with JSDoc param:
alexrock commentedon Oct 25, 2016
The Pool type was not imported in the module, it is visible as pg.Pool
aluanhaddad commentedon Oct 26, 2016
Alexandro Rocha (@alexrock) nice catch.
Nathan Shively-Sanders (@sandersn) really glad things are staying that way. jsdoc comments are often out of sync with the actual code. Also they are quite ugly. I have a colleague who uses JetBrain's Webstorm and it auto emits all these meaningless jsdoc tags into a pure typescript project... sorry for the rant.
timty commentedon Oct 26, 2016
Alexandro Rocha (@alexrock) Aluan Haddad (@aluanhaddad)
It was imported via nodes require statement on the top line. Correct? I do get intellisense in this file for the pg.Pool type just not for my var and function parameter.
aluanhaddad commentedon Oct 26, 2016
Tim York (@timty) you imported the module with the binding
pg.That means that you must qualify all references to exports of the module with
pgas inpg.Pool. The issue is thatPoolis not in scope.some options:
A
B
timty commentedon Oct 26, 2016
Aluan Haddad (@aluanhaddad) Thanks for the feedback. Got it working with Option B...
However for the sake of completeness this does not seem to work...
sandersn commentedon Oct 26, 2016
I don't think that the language service has special knowledge of the
var x = require('pg')pattern, so it doesn't try to resolve 'pg' as a module. Vladimir Matveev (@vladima) or Andy (Andrewkraft) (@Andy-MS), can you confirm?It does understand ES module syntax and the typescript-only
import x = require('...')that Aluan Haddad (@aluanhaddad) mentioned.Tim York (@timty) it's good that you can use ES module syntax to work around this. Does Option B need both lines to work?
I'll leave this bug open until we decide whether the language service should also support
var x = require('...')in Javascript files.[-]Intellisense not respecting jsdoc parameter type for typescript[/-][+]module not resolved in JS file using `var x = require('...')` syntax[/+]sandersn commentedon Oct 26, 2016
Andy (Andrewkraft) (@Andy-MS) poked around the code and there is code that looks for
requirein Javascript:collectRequireCalls. So it must be failing to work as intended.4 remaining items
teppeis commentedon Apr 29, 2017
Mohamed Hegazy (@mhegazy)
Yes! In TS 2.3,
checkJswas introduced. So this feature is more important now.It should work for intellisense and checkJs.
#14377 is another solution for this issue.
enko commentedon May 5, 2017
Vladimir Matveev (@vladima) you wrote the code for
collectRequireCalls. Maybe you can shed some light into this issue? Thanks!mhegazy commentedon May 5, 2017
I do not think we should treat values as types or namespaces; this will just lead to confusion in the future. #14377 would be the way we would proceed with this request.
teppeis commentedon May 6, 2017
Mohamed Hegazy (@mhegazy) I agree.
Treating values as types requires
const type = require('only-for-typing')in JS code.#14377 is better solution.
mhegazy commentedon Nov 28, 2017
Seems like another variant of #16489