'VsCode clean Typescript project no auto import suggestions
When I create a new Node project using Typescript, VsCode auto import suggestions do not work at all.
Steps to reproduce:
Create a workspace directory.
Run npm init specifying main.ts as entrypoint file.
Import typescript cli npm i typescript
Create tsconfig.json .\node_modules\.bin\tsc --init
Create main.ts containing console.log('Running');
Transpile using .\node_modules\.bin\tsc -w
Run by clicking Debug in VsCode and using the default Nodejs launch config.
Import a library e.g. Rxjs Import typescript cli npm i rxjs
Result:
In main.ts attempting to use any Rxjs type Observable, BehaviourSubject, global operators from of sequenceEqual etc results in no import assistance whatsoever.
I've read the VsCode Typescript docs, there is no hint there of what is wrong. https://code.visualstudio.com/docs/languages/typescript
I've tried explicitly setting include and exclude directories in tsconfig.json which also has no effect.
Do I need to manually set some module resolution options in tsconfig or something.
I have no idea, I'm at a loss as to why this doesn't work.
UPDATE:
I managed to get VsCode auto import working by specifying
"typeRoots": [ "node_modules" ]
...in tsconfig.json
The results in a minor issue which is that tsc now reports the error
error TS2688: Cannot find type definition file for '.bin'.
It appears the tsc now tries to read the contents of node_modules.bin which is not helpful. I've tried setting
"exclude": [ "./node_modules/.bin"]
in tsconfig, but that has no effect.
Solution 1:[1]
Try giving main.js as your entrypoint not main.ts
Steps that I followed:
Create a workspace directory.
Run npm init specifying index.js as entry point file.
Install typescript cli: npm i typescript
Create tsconfig.json: .\node_modules\.bin\tsc --init
Create index.ts containing console.log('Running');
Transpile using .\node_modules\.bin\tsc -w
Run by clicking Debug in VSCode and using the default Nodejs launch config.
Install a library e.g. Rxjs: npm i rxjs
Now in index.ts file: import { observable, BehaviorSubject } from "rxjs";
I was able to get those IntelliSense(import assistance)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chirag Shah |
