Solvedatom 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!

28 Answers

✔️Accepted Answer

Just in case someone else falls into the same issue, here's how you'd exclude node_modules and use node_modules/@types:

Before:

"exclude": [
    "node_modules"
]

After:

"exclude": [
    "node_modules",
    "!node_modules/@types"
]

If you simply remove the node_modules from the exclude array, Typescript will try to compile your code and everything else that's in node_modules.

Other Answers:

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/@types to exclude results in issue #1132 for me, I tried following that documentation and specifying typeRoots: ['./node_modules/@types'] instead, inside compilerOptions.

tsc still works, but unfortunately, atom-typescript does even worse then. Besides still failing to resolve any global variables (like describe and it), it also now outputs hundreds of false compilation errors like "Property status does not exist on type Response", where Response is an interface we explicitly import. (It extends Express.Response, which we also explicitly import, in case that's helpful.)

So at this point, it looks like my options are:

  1. Add import {} from 'mocha', 'node', etc. anywhere we use globals.
  2. Add !node_modules/@types to exclude, and hit issue #1132.
  3. Add 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!

I'm working on an update to make atom-typescript behave exactly like running tsc from command line would (basically, by using tsserver that ships with the version of Typescript you have installed in your node_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 😺