TypeScript: Problems using types installed in node_modules/@types
TypeScript Version: 2.0.0-beta
We are encountering various problems using types installed into node_modules/@types. My understanding was that, when types were installed there, the compiler would automatically pick them up, but that does not seem to happen.
If I install @types/angular, then try to use angular-cookies:
import * as angular from 'angular';
import 'angular-cookies';
let cookiesService: angular.cookies.ICookiesService;
I get the following:
error TS2305: Module 'angular' has no exported member 'cookies'.
This is despite angular-cookies.d.ts defining an angular.cookies namespace.
If I install @types/rx, then try to import rx-lite:
import Rx from 'rx-lite';
It cannot find the module:
error TS2307: Cannot find module 'rx-lite'.
The only way I’ve found to fix this is to add an explicit entry to tsconfig.json types:
types {
"rx/rx.lite.d.ts"
}
I thought the compiler included everything in node_modules/@types automatically, so why do I have to add this explicitly?
Similar to the above, if I install @types/tinycolor, which declares a module “tinycolor2” in its index.d.ts, then try to use it:
import * as tinycolor from 'tinycolor2';
It cannot find the module:
error TS2307: Cannot find module 'tinycolor2'.
Again, the only way to make it pick up this is to add an explicit entry to tsconfig.json’s types section.
I’m not sure what the benefit of @types is, if we always have to add explicit config entries for our ambient type definitions in order for the compiler to consider them?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 43
- Comments: 67 (25 by maintainers)
Commits related to this issue
- Fix: build error missing TS module resolution type https://github.com/Microsoft/TypeScript/issues/9725#issuecomment-233481251 — committed to petersandor/bufet by petersandor 7 years ago
For
nodeyou will need to add a/// <reference type="node" />to one of your files or add to your tsconfig.json:breaking it up is not an option here since these are really some magical modules that node injects into the module space.
angular-mockson the other hand is really an npm module (https://www.npmjs.com/package/angular-mocks).I should add. my statement above assumes that your
tsconfig.jsonis in the same folder as yournode_modules\@types. if that is the case, the contents ofnode_modules\@typesare picked up automatically. sonode_modules\@types\mocha\index.d.tswill be in your scope, and thusdescribewould be as well. By default all the contents ofnode_modules\@typesadjacent to yourtsconfig.jsonare included in your compilation.if however, your
tsconfig.jsonis not in the same folder, either a sibling folder, or in a child then you need to specify where to find it to get the same behavior. you can do this by specifying thetypeRootsproperty. e.g.:for a file layout as:
In case you want to say “Do not include all
node_modules\@types” , to do this you can specify a list of declarations you want to specify, e.g.:this will only include
node_modules\@types\node\index.d.tsandnode_modules\@types\mocha\index.d.ts. if you have an emptytypeslist in your tsconfig.json, then nothing is included.hope this helps.
same for me with
@types/node,@types/mochaWith DefinitelyTyped classic (master branch)
nodedefinitions everything is ok.For example for fs module i have import like
For require and global i have no imports.
For
mochaglobal function likedescribe,iti have no imports too.It will work with
but current definitions works without import
https://github.com/sanex3339/javascript-obfuscator/blob/dev/test/config/config.spec.ts#L1
i.e. using the latest TS in VS Code, see https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Nightly Builds.md#visual-studio-code
With https://github.com/DefinitelyTyped/DefinitelyTyped/pull/10170, you should be able to do:
or
"moduleResolution": "node"fixed this for me.@brunolm - Your solution works for me… Thanks a lot… The solution which i tried as per your suggestions is below :
In tsconfig.json : Corrected typeRoot values like following -> “typeRoots”: [“node_modules/@types”]
{ “compilerOptions”: { “module”: “commonjs”, “target”: “es6”, “noImplicitAny”: false, “sourceMap”: false, “typeRoots”: [“node_modules/@types”] }, “exclude”: [ “node_modules” ] }
just as a clarification. the
@typespackages has new TS syntax likeexport as namespaceand/// <reference types=.../>that are added in TS 2.0. By default VSCode comes with TS 1.8. so you need to let VSCode knows to use a different version.That’s because you are now explicitly setting
--lib. Once you do that, you need to add"dom"if you depend on its types directly or transitively (as in this case via jQuery).I’m having trouble with sub-dependencies of libraries that are properly referenced node_modules@types. I think its unexpected that the @types process requires that every sub-library library referenced by an @types/library-name also have an @types d.ts somewhere?
The result is (for me) a long list of build errors like this:
Build:Cannot find type definition file for ‘pinkie-promise’.
I’m guessing ‘pinkie-promise’ is a library required by one of the @types libraries. If it’s a sub-dependency of one of the ‘normal’ dependencies, I’ll have other questions.
here’s the devDependencies and dependencies area of my package.json:
Just the hack I needed too!
Structure
tsconfig.json
webpack.config.js
Dependencies:
On VSCode and webpack it fails, but if I try to build the test file directly:
or
tsc -p .It works.
If I do a
typings install --global mochainstead ofnpm i -D @types/mochathen both VSCode and Webpack work correctly.@aluanhaddad Awesome, that solved it. Many thanks!
use
--lib es6instead.I’m having random problems with TS 2.1.5 on VS 2015. Some of the dependencies inside @types are seen by Visual Studio, some aren’t. This happens sometimes only, and I cannot pinpoint any specific reason. Yesterday things were working fine, put my laptop to sleep, today VS cannot see some types. Restarted VS and nothing.
Compilation using gulp works fine every time, but VS is very unstable.
Is there any way to debug which declarations VS is looking at?
I’m facing problems too (related to
node_modules/%40types/node/index.d.ts') in my Electron project. These messages are already shown in VSCode:package.json
@klemenoslaj please add a path mapping entry as well:
I tried everything above with
Angular 2.2.3and this was what finally worked for me.Add
typings.d.tsto yoursrcfolder with:i am getting error unknown compiler option types. but while checking tsc -v i am getting Version 2.0.0
this is working for me with latest TS in VSCode. the webpack seems like a ts-loader issue.