microsoft / TypeScript-Handbook

Deprecated, please use the TypeScript-Website repo instead
https://github.com/microsoft/TypeScript-Website
Apache License 2.0
4.88k stars 1.13k forks source link

typeRoots falls back to automatic inclusion of packages #774

Open expressiveco opened 6 years ago

expressiveco commented 6 years ago
mhegazy commented 6 years ago

However, when types could not be found in the compilerOptions.typeRoots folders, compiler falls back to the automatic inclusion of @types packages.

that is not accurate.. there are two parts of the process. there are global pacakges (e.g. node, jquery, etc..) and there are modules (e.g. react).

typeRoots controls the global side of things.. that means @types\node will be included even though you never have an import in your project like import ... from "node".

for modules, e.g. import .. from "react", module resolution will go through the normal process, and will try to find a module react. part of that is to fall back to node_modules\@types\react if non better was found.

expressiveco commented 6 years ago

@mhegazy it is already about automatic inclusion of global packages, not about modules imported by import statements.

So, import statements fallback to node_modules\@types but also it fallback when types in typeRoots folders do not exist, even though it is not that significant.

mhegazy commented 6 years ago

but also it fallback when types in typeRoots folders do not exist,

i do not think this is true.

expressiveco commented 6 years ago

@mhegazy I tested it, will check again.

mhegazy commented 6 years ago
c:\test\774>tree /f .
│   a.ts
│   package.json
│
└───node_modules
    └───@types
        └───node
                index.d.ts
                inspector.d.ts
                LICENSE
                package.json
                README.md

c:\test\774>tsc --v
Version 2.9.1

c:\test\774>tsc --listFiles a.ts
C:/Users/mhegazy/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts                                               a.ts
c:/test/774/node_modules/@types/node/inspector.d.ts
c:/test/774/node_modules/@types/node/index.d.ts

c:\test\774>tsc --typeRoots ./a --listFiles a.ts
C:/Users/mhegazy/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts
a.ts
expressiveco commented 6 years ago

Try this;

tsc --typeRoots ./someEmptyFolder --listFiles a.ts
mhegazy commented 6 years ago
c:\test\774>tree /f .
│   a.ts
│   package.json
│   tsconfig.json
│
├───emptyFolder
└───node_modules
    └───@types
        └───node
                index.d.ts
                inspector.d.ts
                LICENSE
                package.json
                README.md

c:\test\774>tsc --typeRoots ./emptyFolder --listFiles a.ts
C:/Users/mhegazy/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts
a.ts

c:\test\774>tsc --listFiles a.ts
C:/Users/mhegazy/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts
a.ts
c:/test/774/node_modules/@types/node/inspector.d.ts
c:/test/774/node_modules/@types/node/index.d.ts
mhegazy commented 6 years ago

if i were to guess, you have an import somewhere in your file.

expressiveco commented 6 years ago

You are right, it does not fallback with an empty typeRoots folder. So, i find out what i was doing. I have an empty sub-folder with my modules name under the typeRoots folder so, it causes the module folder resolved by fallback to the node_modules/@types.

tree /f .
D:/TestProject
│   tsconfig.json
│
├───node_modules
│   └───@types
│       └───mymodule
│               index.d.ts
│
├───someFolder
│   └───mymodule
└───wwwroot
    └───js
            site.ts

- wwwroot/js/site.ts

var  a  =  myClass;

tsc --listFiles --traceResolution --typeRoots someFolder ./wwwroot/js/site.ts ======== Resolving type reference directive 'mymodule', containing file 'D:/TestProject/inferred type names.ts', root directory 'someFolder'. ======== Resolving with primary search path 'someFolder'. File 'someFolder/mymodule/package.json' does not exist. File 'someFolder/mymodule/index.d.ts' does not exist. Looking up in 'node_modules' folder, initial location 'D:/TestProject'. File 'D:/TestProject/node_modules/mymodule.d.ts' does not exist. File 'D:/TestProject/node_modules/@types/mymodule/package.json' does not exist. File 'D:/TestProject/node_modules/@types/mymodule.d.ts' does not exist. File 'D:/TestProject/node_modules/@types/mymodule/index.d.ts' exist - use it as a name resolution result. Resolving real path for 'D:/TestProject/node_modules/@types/mymodule/index.d.ts', result 'D:/TestProject/node_modules/@types/mymodule/index.d.ts'. ======== Type reference directive 'mymodule' was successfully resolved to 'D:/TestProject/node_modules/@types/mymodule/index.d.ts', primary: false. ======== C:/Users/exp/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts wwwroot/js/site.ts D:/TestProject/node_modules/@types/mymodule/index.d.ts

mhegazy commented 6 years ago

Yes. the assumption here is that every subfolder in your typesRoot is a declaration package and not a random folder.