question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typeRoots and types not working

See original GitHub issue

TypeScript Version: 3.8.3

Code This is my tsconfig.json

{
    "compilerOptions": {
        "typeRoots": [
            "./typings"
        ],
        "types": ["Phaser"]  
    }    
}

In my project root, I got package.json, package-lock.json, main.ts(empty one), tsconfig.json.

tsconfig.json contains the above code.

typings folder which I wrote in typeRoots is empty.

After that, I run the following command in terminal:tsc --traceResolution

The idea is I am trying to use Phaser as a global variable in my main.ts file.

Bug 1)

Expected behavior:

What should happen is Phaser shouldn’t get resolved because compiler should start looking into my custom folder typings.

Actual behavior: Somehow it still gets resolved. And this is what it prints: Type reference directive 'Phaser' was successfully resolved to '/Applications/XAMPP/xamppfiles/htdocs/phaser-test/node_modules/Phaser/types/phaser.d.ts' with Pac kage ID 'phaser/types/phaser.d.ts@3.22.0', primary: false. .

I don’t know how it finds it since in my typeRoots, I don’t have node_modules specified.

What I have tried:

I tried to exclude node_modules folder, I thought maybe they still get compiled and that’s why this happens, but I couldn’t make exclude work.

"exclude": ["node_modules/*"]

"exclude": ["./node_modules/*"]

"exclude": ["./node_modules"]

but none of these worked. , but i don’t think that will help because i created one file test.ts and put something like this in there, but compilation is successful, So I guess the bug is related to typeRoots.

let a:number = 10;
a="gio";

Bug 2)

Let’s change the above tsconfig.json to the following:

     "compilerOptions": {
        "typeRoots": [
            "./node_modules/phaser/types",
        ],
        "types": ["Phaser"]  
    },

What should happen now is Phaser should be resolved from ./node_modules/phaser/types and not take care of anything else such as trying to find package.json in node_modules/phaser and looking at types property in that package.json. But what actually happens is this doesn’t get resolved at all on Ubuntu versions, since ubuntu is 'case-sensitive. This config works fine on OS x and windowsbecausePhasergets resolved fromnode_modules/phaser/package.json’s types propertywhich shouldn't be happening because of mytypeRoots`.

Basically both things are so much related to each other.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:15
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
shadow-lightcommented, Aug 20, 2021

I have @types/node installed in top level dir, and a tsconfig.json for a sub-component in a subdir with "types": [] and "typeRoots": [] but node types are still loaded in my browser app (__dirname etc). This seems to contradict what the docs say:

By default all visible ”@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible. For example, that means packages within ./node_modules/@types/, …/node_modules/@types/, …/…/node_modules/@types/, and so on. If typeRoots is specified, only packages under typeRoots will be included.

https://www.typescriptlang.org/tsconfig#typeRoots

I assume this is the same issue?

3reactions
maitlandmarshallcommented, Aug 10, 2022

I’m working on a web app and have @types/node installed from a transient dependency. I have types set to an empty array in compiler options and it still uses the node types.

I’m also experiencing the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typeRoots is not finding custom declaration file #22217 - GitHub
This seems to be an issue because tsc is not finding the type declaration in the directories specified in typeRoots. Related Issues:
Read more >
tsconfig.json typeroots custom path not picked up
To me, typeRoots does not work. For a temp workaround I added a postinstall script which copies the types from my nested directory...
Read more >
typeRoots - TSConfig Option - TypeScript
TSConfig. typeRoots ... How to provide a type shape to JavaScript objects. Narrowing. How TypeScript infers types based on runtime behavior.
Read more >
TypeScript compiler compile all ignores tsconfig.json ...
The TypeScript compiler seems to be ignoring the tsconfig.json typeRoots option. ... So far we could not reproduce the problem with the simple...
Read more >
Using tsconfig.json - TypeScript
If typeRoots is specified, only packages under typeRoots will be included. For example: ... Other packages under node_modules/@types/* will not be included.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found