Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get VScode recognize d.ts files without referencing them directly

I have a project created with create-react-app-ts

I also have a set of d.ts files that are generated interfaces from JSON-schema. They define some interfaces for a remote API.

I would like these d.ts file to be "globally" available throughout the project, without the need to directly reference them by file name. Somewhat similar to how Promise<T> definition is available globally.

I've tried modifying tsconfig, added path/to/**/*.dts to include, as well as in files. I also tried adding path to "typeRoots" under compilerOptopns.

This makes the project compile, but VSCode provides no IntelliSense to these interfaces, and underlines them as unknown.

like image 829
dark_ruby Avatar asked Oct 10 '18 19:10

dark_ruby


People also ask

How do I compile a TS file in Vscode?

To compile your TypeScript code, you can open the Integrated Terminal (Ctrl+`) and type tsc helloworld. ts . This will compile and create a new helloworld. js JavaScript file.

How do I select TypeScript version in VS code?

json file. The typescript. tsdk workspace setting only tells VS Code that a workspace version of TypeScript exists. To actually start using the workspace version for IntelliSense, you must run the TypeScript: Select TypeScript Version command and select the workspace version.

How do I add a TypeScript code in Visual Studio?

Visual Studio Code includes TypeScript language support but does not include the TypeScript compiler, tsc . You will need to install the TypeScript compiler either globally or in your workspace to transpile TypeScript source code to JavaScript ( tsc HelloWorld. ts ). You can test your install by checking the version.

How do I enable IntelliSense code in Visual Studio?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript).


2 Answers

If TypeScript compiles without errors, but VSCode still can't recognize global types and add proper intellisense, then it's likely that you have a project with too many files and you are not being specific enough about the files you want TypeScript to check.

To add intellisense to global types, VSCode iterates over all files available, since you did not specify which ones to check, or was too broad about it. With too many files, VSCode stops looking around.

Using typeRoots to declare your global types folder/files should be enough for VSCode to spot what files it should look for. If it doesn't happen, consider specifying all the paths you expect intellisense to work in the include field, or remove all the files you don't want in the exclude field. VSCode should follow and add intellisense to the missing files.

{
  "compilerOptions": {
    "typeRoots": [
      "path/to/**/*.d.ts"
    ],
    "include": [
      // Add all paths you want intellisense to work
      "path/to/where/you/expect/intellisense/to/work",
      "path/to/another/file/or/folder"
    ]
  }
}
like image 196
Cezar Augusto Avatar answered Oct 02 '22 13:10

Cezar Augusto


Based on tsconfig compiler options, you should use option typeRoots. This section of documentation describes the difference between @types, typeRoots and types.

For your specific case this tsconfig.json file should do the trick:

{
    "compilerOptions": {
        "typeRoots" : ["./type-definitions-folder"]
    }
}

If you are also using type definitions from npm you should also add ./node_modules/@types.

{
    "compilerOptions": {
        "typeRoots" : ["./node_modules/@types", "./type-definitions-folder"]
    }
}
like image 38
dotnetom Avatar answered Oct 02 '22 13:10

dotnetom