atom-typescript: [typescript2] - Doesn't recognize types installed by npm i @types/something
First of, amazing job with this package.
So, I have this new project that I’m using typescript@2 and I decided to give @types a shot too.
I have this test file that depends on jasmine, so I installed the types for it by running npm i --save-dev @types/jasmine. It has the following content:
// my_app_test.ts
describe('my-app', () => {
console.log('yo');
});
If I run tsc, it compiles just fine, no warnings, no errors - nothing. But, Atom keeps telling me Cannot find name 'describe'.. So I figured it should be related to atom-typescript.
Info
- os:
windows 7, 64bits - atom:
1.9.8 - atom-typescript:
10.1.6 - tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"sourceMap": false
},
"exclude": [
"node_modules"
]
}
Thanks!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 27 (7 by maintainers)
Commits related to this issue
- Added note about #1054 to FAQ — committed to code-tree/atom-typescript by code-tree 8 years ago
- Merge pull request #1159 from code-tree/patch-1 Added note about #1054 to FAQ — committed to TypeStrong/atom-typescript by guncha 7 years ago
- Add explicit types until atom bug is fixed - https://github.com/TypeStrong/atom-typescript/issues/1054 - https://github.com/TypeStrong/atom-typescript/pull/1159/files - https://github.com/TypeStrong/... — committed to muuki88/daylight by muuki88 7 years ago
Just in case someone else falls into the same issue, here’s how you’d exclude
node_modulesand usenode_modules/@types:Before:
After:
If you simply remove the
node_modulesfrom theexcludearray, Typescript will try to compile your code and everything else that’s innode_modules.I’m working on an update to make
atom-typescriptbehave exactly like runningtscfrom command line would (basically, by usingtsserverthat ships with the version of Typescript you have installed in yournode_modules). That will address this and similar issues. If you feel it’s important to document the current behavior, I won’t say NO to a PR 😺People here may already be aware of this, but this was new to me — here is where it’s documented that TypeScript automatically adds everything inside
node_modules/@types:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types
(Note that because of the header on that page, you may have to scroll up a bit.)
Given that adding
!node_modules/@typestoexcluderesults in issue #1132 for me, I tried following that documentation and specifyingtypeRoots: ['./node_modules/@types']instead, insidecompilerOptions.tscstill works, but unfortunately,atom-typescriptdoes even worse then. Besides still failing to resolve any global variables (likedescribeandit), it also now outputs hundreds of false compilation errors like “Propertystatusdoes not exist on typeResponse”, whereResponseis an interface we explicitlyimport. (It extendsExpress.Response, which we also explicitlyimport, in case that’s helpful.)So at this point, it looks like my options are:
import {} from 'mocha','node', etc. anywhere we use globals.!node_modules/@typestoexclude, and hit issue #1132.typeRoots: ['./node_modules/@types']and hit more errors.Clearly 1 is the least bad option, but it’s sad that it’s only needed for
atom-typescript.Please let me know if there’s any other info I can provide to help. Thank you again!
@jhm-ciberman, I’m glad you asked! #1166
I’m on the latest of everything pretty much (Atom-TypeScript 10.1.12, Atom 1.12.2, TypeScript 2.0.9, Node 6.9.1, etc.) and yes, this is an issue for me.
High-level repro: create a project with
exclude: ['node_modules']intsconfig.json,npm install --save-dev @types/mocha, and write a newtest/foo.tsfile that referencesdescribeandit. VS Code doesn’t complain, it compiles fine w/tsc, and runs fine w/mocha --compilers ts:ts-node/register, but Atom-TypeScript complains thatdescribeanditare unknown.Adding
'!node_modules/@types'toexcludelike @ericmdantas suggested fixes this for me.I actually don’t know (can’t explain) why only Atom-TypeScript fails here and others don’t also. The
mochatypings certainly declaredescribe,it, etc. as globals, but I’m not quite sure how this typing gets picked up whenmochaisn’t explicitlyimported, andnode_modulesin general isexcluded.Alas, I think the biggest issue here is simply that Atom-TypeScript is differing from every other tool.
One can argue about if this is a bug or not. It definitely shows that atom-typescript diverges from the standard tsc compiler results.
There are multiple issues regarding more or less to the same problem: https://github.com/TypeStrong/atom-typescript/issues/1055
You’re excluding all of node_modules, which is where it would store the types…?
My short term fix has been to leave out the
excludesfrom my tsconfig, leaning on the defaults (since all I had in there was node_modules anyway):Then I’ve been able to define my custom
typesandtypeRootsproperty without complaint from atom-typescript.Atom-typescript just seems to hate the interaction between
typeRootsandexcludes.@guncha
Thanks, can this at least be documented in the readme, since as you noted, Typescript docs say
Specify "types": [] to disable automatic inclusion of @types packages.If atom-typescript doesn’t behave like this, then it should at least be documented.thanks
I ran into this same problem and have used
import {} from 'mocha'in my code to resolve.The silver lining is that you only need to do this once, so I have a
typings.tsfile and put this at the top along with other imports:And that seems to resolve it for all test files.
Thanks to @aseemk for listing the options.
Thanks for debugging. Yes, it seems that Typescript automatically adds everything inside
node_modules/@types/*to the compilation context, but only if you haven’t specified any value fortypesincompilerOptions.This is done in
ts.createProgramwhileatom-typescriptis usingts.createLanguageServiceto interface with the Typescript compiler. Makes me wonder if other editors have the same issue and why the Typescript people set it up this way.Hey @guncha, thanks for the response. I want to clarify a few things:
I’m sorry if this wasn’t clear: I am excluding it, and was not suggesting otherwise. I was talking about explicitly adding
!node_modules/@typestoexclude, as @ericmdantas suggested here:https://github.com/TypeStrong/atom-typescript/issues/1054#issuecomment-240509189
I understand I have these two options. And I completely get your reasoning — it makes sense. But the key issue to me is that I don’t need to do either of these for
tsc,ts-nodeand VS Code. Why?This might be a start! They indeed appear different.
I don’t have
tscinstalled globally at all; I only have it installed locally in my project via a localtypescriptdependency. It looks like Atom-TypeScript does this too, and the versions differ:I manually set Atom-TypeScript to point to my 2.0.3
/full/path/to/node_modules/typescript/lib/typescriptServices.js, ran into issue #1124, so followed theloopholeworkaround. Unfortunately, if I remove!node_modules/@typesfromexclude, I still see this issue (of unrecognizeddescribeanditerrors). (And if I don’t remove!node_modules/@typesfromexclude, I still see issue #1132.)Again, I’d just like to emphasize: I’d only like to understand why Atom-TypeScript doesn’t behave the same as
tsc,ts-node, and VS Code. Thank you again.Sorry, my bad. Tested it with the wrong packages. Looks like type definitions that provide globals like mocha, etc. are affected only.
The reason for atom-typescript’s different behavior is most likely the way it sets up the “Typescript Language Service”. I started to debug this issue once but haven’t found any time to do dig deeper.
Btw, I also experienced this with
@types/nodefor e.g.require: Atom-TypeScript complains thatrequireisn’t known, but no other tool does.Thank you @ericmdantas for the tip! That fixes this for me too.
I was fighting with this behavior for months now, and I agree it feels like a bug since VS Code,
tsc, andts-nodeall work fine without having to explicitly includenode_modules/@types.Why does
atom-typescriptdiffer in behavior like this? Is there any specific reason?Thanks for the great work on this plugin in general. I hope my comment doesn’t come across as ungrateful. =) Cheers.