David Callanan
David Callanan

Reputation: 5958

tsc cannot find name `Set` with jest

I have typescript compilation setup, and compiling works perfectly.

I then install jest (a testing framework) as a devDependency. This is for development only (hence why it is a devDependency), and shouldn't be part of the compiled package.

Compilation now results in the following error:

node_modules/@types/babel__template/index.d.ts:16:28 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

16     placeholderWhitelist?: Set<string>;

Edit: tsconfig.json

{
  "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "rootDir": "./src",
        "outDir": "./build",

        "strict": true,
        "esModuleInterop": true
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "/src/**/?(*.)test.ts"
    ]
}

Note: This is not a duplicate of node_modules/@types/babel _template/index.d.ts :16:28 - error TS2583: Cannot find name 'Set'

This question was asking why tsc is looking in node_modules which is different from my questions (and the accepted answer is for that question).

Upvotes: 3

Views: 923

Answers (2)

Shaun Luttin
Shaun Luttin

Reputation: 141462

Why is tsc compiling devDependencies and how do I stop this? I don't see why I should suddenly have to worry about the nested babel devDependency affecting my compilation.

By default, tsc loads all the declarations in node_modules/@types/**/*, which in your case, includes a type declaration that depends on >= es2015. If that type declaration came from a devDependency, then tsc will include it. That's why tsc is compiling something associated with a devDependency.

If upgrading to es2015 is not a valid solution, then you can also try ignoring the specific type declaration that depends on it.

From the TypeScript documentation on @types:

By default all visible “@types” packages are included in your compilation... Specify "types": [] to disable automatic inclusion of @types packages.

Upvotes: 2

Richard Haddad
Richard Haddad

Reputation: 1004

Considering your tsconfig you can:

  • add lib to your compilerOptions, but you don't want impact your project because of a test lib, I understand that;

  • make sure your tests use another tsconfig, like a tsconfig.test.json which have the needed lib => I think it's your solution;

Upvotes: 1

Related Questions