avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Typescript: don't force @types/node to be included #2397

Closed Retsam closed 4 years ago

Retsam commented 4 years ago

I've got a browser app, and I'm trying to ensure that @types/node aren't included as part of typescript's compilation environment. I had this working with AVA v2, but AVA v3 is forcing @types/node, causing compilation issues.

Here's a example:

// src/foo.ts
export default function() {
    // If @types/node is loaded, this errors on compilation 
    //   with "NodeJS.Timeout is not assignable to number"
    let timeout: number = setTimeout(() => console.log("Ping"), 2000);
}
// tests/foo.test.ts
import test from "ava";
//...
// tsconfig.json
{
    "compilerOptions": {
        // Prevent TS from automatically loading ambient definitions from node_modules/@types
        // except those explicitly listed here
        "types": [],
    },
    "include": ["src"],
}

// tsconfig.test.json
{
    "extends": "./tsconfig",
    "include": ["src", "tests"]
}

The project compiles correctly with tsconfig.json but fails with tconfig.test.json, because @types/node gets pulled in. The culprit is this line, which was added by #2154:

/// <reference types="node"/>

That causes @types/node to be pulled in explicitly, and foo.ts fails to compile. (I've confirmed that removing that line from node_modules/ava/index.d.ts fixes the compilation)


The only workaround I could come up with from my end, (other than adding @types/node to my non-test code and changing the annotations) would be to move to a Typecript project references setup, which is non-trivial.

novemberborn commented 4 years ago

Oh good catch, fixing in https://github.com/avajs/ava/pull/2398.

We don't even use that symbol anymore.